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
SpaceShroomies
  • 1,635
  • 2
  • 17
  • 23
  • 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? – matt May 31 '15 at 14:43
  • What is `cameraStill`? You cannot convert a CIImage to a usable UIImage merely by calling `init(CIImage:...)`. – matt May 31 '15 at 14:45
  • oImage is an image the user took with his camera and cameraStill is a UIImageView which displays the image. – SpaceShroomies May 31 '15 at 14:50
  • But how / when did you obtain the image from the camera roll? My answer will depend upon this. – matt May 31 '15 at 14:52
  • 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? – matt May 31 '15 at 14:54
  • 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? – SpaceShroomies May 31 '15 at 15:53

2 Answers2

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Hi @SpaceShroomies can you show the changes you have done? I'm having the same issue and not understand the solution – Iraniya Naynesh Feb 26 '18 at 12:29
  • 1
    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!); – Iraniya Naynesh Feb 26 '18 at 12:33
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)
Eugene Lezov
  • 722
  • 7
  • 12