If a filter is applied to a PNG where height > width, it rotates the image 90 degrees. How can I efficiently prevent this?

倾然丶 夕夏残阳落幕 提交于 2019-12-13 18:12:50

问题


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)!)}

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

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.

Any advice would be greatly appreciated!

EDIT: heres where I got the method I was trying: When applying a filter to a UIImage the result is upside down


回答1:


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

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
    }

This code replaces the if let output in the OP.



来源:https://stackoverflow.com/questions/43528115/if-a-filter-is-applied-to-a-png-where-height-width-it-rotates-the-image-90-de

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!