1

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

Community
  • 1
  • 1
genericguy25
  • 602
  • 3
  • 16
  • I personally like and have used this answer: http://stackoverflow.com/a/41247037/6855532 The extension is a clean approach. – karnett Apr 20 '17 at 19:22
  • Thank you, I tried converting my CIimage to a UIimage like this: let image = UIImage(CIImage: ciimage) to use the extension you gave and now the conversion is giving me an ambiguous use of 'init(CIImage:) error'. any ideas on how to convert the Ciimage to uiimage correctly? – genericguy25 Apr 20 '17 at 19:29
  • Thanks I also need help converting the CIImage I'm working with to a uiimage to use that. Any recommendations? – genericguy25 Apr 20 '17 at 19:30
  • http://stackoverflow.com/a/42591183/2303865 – Leo Dabus Apr 20 '17 at 19:31
  • I forgot about this one you can use the CIImage image property extension to render your climate into an usable UIImage http://stackoverflow.com/a/43011878/2303865 – Leo Dabus Apr 20 '17 at 23:03
  • Im not sure how to use the first extension you recommended me: http://stackoverflow.com/questions/42098390/swift-png-image-being-saved-with-incorrect-orientation/42098812#42098812 usually when i have an extension there is a function I can link to and use in my view controller. the one you linked only has vars. – genericguy25 Apr 21 '17 at 01:24

1 Answers1

0

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.

genericguy25
  • 602
  • 3
  • 16