Search code examples
iosswiftxcodecore-image

iOS: When applying a CIFilter to a UIImage the result image is rotated


When i apply CIFilter to a image that is captured from Back camera the result is perfect but When i apply CIFilter to a image that is captured from Front camera the result is not good because image rotated whats wrong there??

My filter function is given below

func ApplyFilter()->UIImage
{
    let ciContext = CIContext(options: nil)
    let coreImage = CIImage(image: myImage!)
    let filter = CIFilter(name: "CIPhotoEffectNoir" )
    filter!.setDefaults()
    filter!.setValue(coreImage, forKey: kCIInputImageKey)
    let filteredImageData = filter!.valueForKey(kCIOutputImageKey) as! CIImage
    let filteredImageRef = ciContext.createCGImage(filteredImageData, fromRect: filteredImageData.extent)
    var newImage = UIImage(CGImage: filteredImageRef);

    return newImage

}

Solution

  • Make sure the image size before apply filters means width should be less than height because image/photos taken from front camera have grater width than length that may cause problem after applying filters. https://developer.apple.com/library/content/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Cameras/Cameras.html