UIImageView contentMode not working after blur effect application

I am trying to set an image property UIImageViewon an image that I am blurring with CoreImage. The code works fine with the unfiltered image, but when I set the background image to the filtered image, it contentModestops working for UIImageView- instead of filling out the aspect, the image becomes vertically stretched. In addition to installing contentModein the code, I also installed it on the storyboard, but the result was the same.

I am using Swift 2 / Xcode 7.

func updateBackgroundImage(image: UIImage) {
    backgroundImage.contentMode = .ScaleAspectFill
    backgroundImage.layer.masksToBounds = true
    backgroundImage.image = blurImage(image)
}

func blurImage(image: UIImage) -> UIImage {
    let imageToBlur = CIImage(image: image)!

    let blurfilter = CIFilter(name: "CIGaussianBlur")!
    blurfilter.setValue(10, forKey: kCIInputRadiusKey)
    blurfilter.setValue(imageToBlur, forKey: "inputImage")

    let resultImage = blurfilter.valueForKey("outputImage") as! CIImage
    let croppedImage: CIImage = resultImage.imageByCroppingToRect(CGRectMake(0, 0, imageToBlur.extent.size.width, imageToBlur.extent.size.height))
    let blurredImage = UIImage(CIImage: croppedImage)

    return blurredImage
}

Why does filtering with CIImagemake my image ignore contentModeand how can I fix this problem?

+4
1

:

let blurredImage = UIImage(CIImage: croppedImage)

:

let context = CIContext(options: nil)
let blurredImage = UIImage (CGImage: context.createCGImage(croppedImage, fromRect: croppedImage.extent))

, blurImage :

func blurImage(image: UIImage) -> UIImage {
    let imageToBlur = CIImage(image: image)!

    let blurfilter = CIFilter(name: "CIGaussianBlur")!
    blurfilter.setValue(10, forKey: kCIInputRadiusKey)
    blurfilter.setValue(imageToBlur, forKey: "inputImage")

    let resultImage = blurfilter.valueForKey("outputImage") as! CIImage
    let croppedImage: CIImage = resultImage.imageByCroppingToRect(CGRectMake(0, 0, imageToBlur.extent.size.width, imageToBlur.extent.size.height))
    let context = CIContext(options: nil)
    let blurredImage = UIImage (CGImage: context.createCGImage(croppedImage, fromRect: croppedImage.extent))

    return blurredImage
}
+5

Source: https://habr.com/ru/post/1607556/


All Articles