【发布时间】:2017-09-17 14:16:34
【问题描述】:
我正在制作一个简单的过滤器应用程序。我发现,如果您从相机胶卷中加载一个 PNG 图像(PNG 没有方向数据标志)并且高度大于宽度,则在对所述图像应用某些失真过滤器时,它将旋转并呈现它自己就好像它是一幅风景图像一样。
我在打开的许多选项卡中的某处在线找到了以下技术,它似乎完全符合我的要求。它使用第一次加载时图像的原始比例和方向。
let newImage = UIImage(CIImage:(output), scale: 1.0, orientation: self.origImage.imageOrientation)
但这是我尝试使用它时收到的警告:
Ambiguous use of 'init(CIImage:scale:orientation:)'
这是我正在努力工作的全部内容:
//global variables
var image: UIImage!
var origImage: UIImage!
func setFilter(action: UIAlertAction) {
origImage = image
// make sure we have a valid image before continuing!
guard let image = self.imageView.image?.cgImage else { return }
let openGLContext = EAGLContext(api: .openGLES3)
let context = CIContext(eaglContext: openGLContext!)
let ciImage = CIImage(cgImage: image)
let currentFilter = CIFilter(name: "CIBumpDistortion")
currentFilter?.setValue(ciImage, forKey: kCIInputImageKey)
if let output = currentFilter?.value(forKey: kCIOutputImageKey) as? CIImage{
//the line below is the one giving me errors which i thought would work.
let newImage = UIImage(CIImage:(output), scale: 1.0, orientation: self.image.imageOrientation)
self.imageView.image = UIImage(cgImage: context.createCGImage(newImage, from: output.extent)!)}
过滤器都可以工作,不幸的是,出于我怀疑的原因,它们将上述图像旋转了 90 度。
我尝试了一些其他方法,例如使用检查 UIimages 方向的扩展程序并将 CIimage 转换为 Uiimage,使用扩展程序,然后尝试将其转换回 Ciimage 或仅将 UIimage 加载到 imageView 以进行输出.我在这个过程中遇到了一个又一个的障碍。我开始似乎真的很费解,只是为了让某些图像也恢复到它们的默认方向。
任何建议将不胜感激!
编辑:这里是我尝试的方法:When applying a filter to a UIImage the result is upside down
【问题讨论】:
-
我个人喜欢并使用过这个答案:stackoverflow.com/a/41247037/6855532 扩展是一种干净的方法。
-
谢谢,我尝试将我的 CIimage 转换为 UIimage,如下所示:让 image = UIImage(CIImage: ciimage) 使用您提供的扩展名,现在转换让我对 'init 的使用模棱两可(CIImage:) 错误'。关于如何正确将 Ciimage 转换为 uiimage 的任何想法?
-
谢谢,我还需要帮助将我正在使用的 CIImage 转换为 uiimage 以使用它。有什么建议吗?
-
我忘了这个你可以使用 CIImage 图像属性扩展来将你的气候渲染成一个可用的 UIImage stackoverflow.com/a/43011878/2303865