Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image rotating after CIFilter

Tags:

swift

ciimage

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
like image 924
SpaceShroomies Avatar asked May 31 '15 14:05

SpaceShroomies


1 Answers

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.

like image 150
matt Avatar answered Nov 01 '22 18:11

matt