Image rotating after CIFilter
Asked Answered
D

2

5

I'm applying a CIFilter to a portrait image. For some reason, it gets rotated 90 clockwise. How can I fix this? My code is below

var imgOrientation = oImage.imageOrientation
var imgScale = oImage.scale


let originalImage = CIImage(image: oImage)

var filter = CIFilter(name: "CIPhotoEffect"+arr[sender.tag-1000])
filter.setDefaults()
filter.setValue(originalImage, forKey: kCIInputImageKey)

var outputImage = filter.outputImage
var newImage = UIImage(CIImage:outputImage, scale:imgScale, orientation:imgOrientation)


cameraStill.image = newImage
Deathday answered 31/5, 2015 at 14:24 Comment(6)
What is oImage? Is the image coming from the Camera Roll? In other words, is this a photo that the user took in portrait orientation? If so, how did you obtain it?Carminecarmita
What is cameraStill? You cannot convert a CIImage to a usable UIImage merely by calling init(CIImage:...).Carminecarmita
oImage is an image the user took with his camera and cameraStill is a UIImageView which displays the image.Deathday
But how / when did you obtain the image from the camera roll? My answer will depend upon this.Carminecarmita
So you are actually seeing the image in your image view? Even though a UIImage generated from init(CIImage:) is not suitable for direct display in an image view?Carminecarmita
I obtain the image from taking from a custom avcamera instance which captures the picture. Why aren't UIImages suited to display? How can I make it suitable?Deathday
C
10

I'm going to guess that the problem is this line:

var newImage = UIImage(CIImage:outputImage, scale:imgScale, orientation:imgOrientation)

That is not how you render a filter into a UIImage. What you want to do is call CIContext(options: nil) to get a CIContext, and then send that CIContext the message createCGImage:fromRect: to get a CGImage. Now turn that CGImage into a UIImage, and, as you do so, you can apply your orientation.

Carminecarmita answered 31/5, 2015 at 16:1 Comment(2)
Hi @Deathday can you show the changes you have done? I'm having the same issue and not understand the solutionOkie
let ciContext = CIContext(options: nil) let coreImage = CIImage(image: originalImage.image!) let filter = CIFilter(name: "(CIFilterNames[i])" ) filter!.setDefaults() filter!.setValue(coreImage, forKey: kCIInputImageKey) let filteredImageData = filter!.value(forKey: kCIOutputImageKey) as! CIImage let filteredImageRef = ciContext.createCGImage(filteredImageData, from: filteredImageData.extent) let imageForButton = UIImage(cgImage: filteredImageRef!);Okie
P
2

You can try this :

let cgImage = self.context.createCGImage(filterOutputImage,
                                         from: cameraImage.extent)!

let orientation: UIImage.Orientation =
      currentCameraType == AVCaptureDevice.Position.front ?
           UIImage.Orientation.leftMirrored:
           UIImage.Orientation.right

let image = UIImage(cgImage: cgImage,
                    scale: 1.0,
                    orientation: orientation)
Pruritus answered 10/8, 2019 at 10:28 Comment(1)
This creates distorted imagesInvitatory

© 2022 - 2025 — McMap. All rights reserved.