如果将过滤器应用于高度 >宽度,它将图像旋转 90 度.我怎样才能有效地防止这种情况? [英] If a filter is applied to a PNG where height > width, it rotates the image 90 degrees. How can I efficiently prevent this?

查看:23
本文介绍了如果将过滤器应用于高度 >宽度,它将图像旋转 90 度.我怎样才能有效地防止这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个简单的过滤器应用程序.我发现,如果您从相机胶卷中加载 PNG 图像(PNG 没有方向数据标志)并且高度大于宽度,则在对所述图像应用某些失真过滤器后,它会旋转并自行呈现好像是风景图.

I'm making a simple filter app. I've found that if you load an image from the camera roll that is a PNG (PNGs have no orientation data flag) and the height is greater than the width, upon applying certain distortion filters to said image it will rotate and present it self as if it were a landscape image.

我在打开的许多选项卡中的某个地方在线找到了以下技术,它似乎完全符合我的要求.它使用图像首次加载时的原始比例和方向.

I found the below technique online somewhere in the many tabs i had open and it seems to do exactly what i want. It uses the original scale and orientation of the image when it was first loaded.

let newImage = UIImage(CIImage:(output), scale: 1.0, orientation: self.origImage.imageOrientation)

但这是我尝试使用它时收到的警告:

but this is the warning i get when i try to use it:

Ambiguous use of 'init(CIImage:scale:orientation:)'

这是我正在努力工作的全部内容:

Here's the entire thing I'm trying to get working:

//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 度.

The filters all work, they unfortunately turn images described above by 90 degrees for the reasons I suspect.

我尝试了一些其他方法,例如使用检查 UIimages 方向的扩展程序并将 CIimage 转换为 Uiimage,使用扩展程序,然后尝试将其转换回 Ciimage 或只是将 UIimage 加载到 imageView 以进行输出.我在这个过程中遇到了一个又一个障碍.我开始似乎真的很费解,只是为了将某些图像也设置为默认方向.

I've tried some other methods like using an extension that checks orientation of UIimages and converting the CIimage to a Uiimage, using the extension, then trying to convert it back to a Ciimage or just load the UIimage to the imageView for output. I ran into snag after snag with that process. I started to seem really convoluted just to get certain images to their default orientation as well.

任何建议将不胜感激!

这里是我尝试的方法:当对 UIImage 应用过滤器时,结果是颠倒的

推荐答案

我找到了答案.我最大的问题是'init(CIImage:scale:orientation:)'的模糊使用"

I found the answer. My biggest issue was the "Ambiguous use of 'init(CIImage:scale:orientation:)' "

结果是 Xcode 自动将代码填充为CIImage:scale:orientation",而它应该是 ciImage:scale:orientation 非常模糊的错误让一个新开发人员像我一样为此挠头 3 天.(CGImage 和 UIImage 初始化也是如此,但我最初的错误是 CIImage 的,所以我用它来解释.)

it turned out that Xcode was auto populating the code as 'CIImage:scale:orientation' when it should have been ciImage:scale:orientation' The very vague error left a new dev like my scratching my head for 3 days over this. (This was true for CGImage and UIImage inits as well, but my original error was with CIImage so I used that to explain.)

凭借这些知识,我能够为我的新输出制定以下代码:

with that knowledge I was able to formulate the code below for my new output:

if let output = currentFilter?.value(forKey: kCIOutputImageKey) as? CIImage{

    let outputImage = UIImage(cgImage: context.createCGImage(output, from: output.extent)!)

    let imageTurned = UIImage(cgImage: outputImage.cgImage!, scale: CGFloat(1.0), orientation: origImage.imageOrientation)
    centerScrollViewContents()
    self.imageView.image = imageTurned
    }

此代码替换了 OP 中的 if let 输出.

This code replaces the if let output in the OP.

这篇关于如果将过滤器应用于高度 >宽度,它将图像旋转 90 度.我怎样才能有效地防止这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆