Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CIFilter has changed UIImage Aspect Ratio - SWIFT

I have implemented a CIFilter to profileImages in my iOS app. I have added orientation to prevent pixelation but I am suffering from the aspect ratio "ScaleAspectFill" not being applied to the images. The same images in colour have the perfect aspect ratio...i.e. when I just have:

self.p1ProfilePic.image = p1Img
  1. I have specified exact height and widths for the profile images in Storyboard.
  2. I have tried adding programmatically the .contentMode as .ScaleAspectFill but with no effect whatsoever.
  3. What am I missing here? CGRect?

            let originalImageP1 = CIImage(image: p1Img!)
            let filter = CIFilter(name: "CIPhotoEffectTonal")
            filter!.setDefaults()
            filter!.setValue(originalImageP1, forKey: kCIInputImageKey)
            let outputImage = filter!.outputImage
            self.p1ProfilePic.image = UIImage(CIImage: outputImage!, scale: UIScreen.mainScreen().scale, orientation: .Up)
    
like image 365
David West Avatar asked Mar 02 '16 02:03

David West


1 Answers

Creating a UIImage from a CIImage with your technique creates images that don't respect contentMode. Try this instead:

let ciContext = CIContext()
let cgImage = ciContext.createCGImage(filter.outputImage!,
    fromRect: filter.outputImage!.extent)

self.p1ProfilePic.image = UIImage(CGImage: cgImage)

Cheers!

Simon

like image 198
Simon Gladman Avatar answered Oct 14 '22 23:10

Simon Gladman