Image rotates after applying filter

Hi,


I am using filter in my project.


When I load image from gallery and apply filter then it works fine. But when I capture an image and apply the filter then it gives me strange result. Captured image gets rotated left by 90 degrees.


It will be helpful if someone can suggest me a better solution. I am using swift version 1.2


Thank you

Answered by NikolaTesla in 217869022

I struggled a bit and found the solution. When you capture images from camera then default orientation is landscape perhaps that was causing the trouble. I just used following swift extention and it worked fine for me.



extension UIImage {

func fixOrientation() -> UIImage {

if self.imageOrientation == UIImageOrientation.Up {

return self

}

UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

self.drawInRect(CGRectMake(0, 0, self.size.width, self.size.height))

let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return normalizedImage;

}

}


and to use it with image:


let capturedImage = //image picked using picker

let orinentationFixedImage = capturedImage.fixOrientation()

Accepted Answer

I struggled a bit and found the solution. When you capture images from camera then default orientation is landscape perhaps that was causing the trouble. I just used following swift extention and it worked fine for me.



extension UIImage {

func fixOrientation() -> UIImage {

if self.imageOrientation == UIImageOrientation.Up {

return self

}

UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

self.drawInRect(CGRectMake(0, 0, self.size.width, self.size.height))

let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return normalizedImage;

}

}


and to use it with image:


let capturedImage = //image picked using picker

let orinentationFixedImage = capturedImage.fixOrientation()

you need the set output.connection(with: .video)?.videoOrientation before capture image

if let orientation = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue) {
    camera.output.connection(with: .video)?.videoOrientation = orientation
}
Image rotates after applying filter
 
 
Q