0

I am making a music player app and I want A gaussian blur on the album cover image so I implemented the code that follows but the image is not filling the UIImageView:

let bgView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.width))
let image = CIImage(image: (currentSong?.info.albumCover)!)
let blurFilter = CIFilter(name: "CIGaussianBlur")
blurFilter?.setValue(image, forKey: "inputImage")
let resultImage = blurFilter?.value(forKey: "outputImage") as! CIImage
let blurredImage = UIImage(ciImage: resultImage)
bgView.image = blurredImage
bgView.contentMode = UIViewContentMode.scaleAspectFit
self.addSubview(bgView)

I thought that changing the content mode to scaleAspectFit would fix that but it did not

Update:

The Code below gives me the desired image size but without the blur. is there any reason applying a blur would cause the size of the image to be different?

let bgView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.width))
    let image = CIImage(image: (currentSong?.info.albumCover)!)
    bgView.image = image
    bgView.contentMode = UIViewContentMode.scaleAspectFit
    self.addSubview(bgView)
Ethan Hardacre
  • 125
  • 2
  • 12
  • 1
    scaleAspectFit will maintain the ratio and " Any remaining area of the view’s bounds is transparent." from apple document. you may try scaleToFill? – Surely Aug 12 '17 at 16:21
  • @Surely I just tried that and got the same result. The aspect ratio for the image and the view are the same as well so that would not make a difference. – Ethan Hardacre Aug 12 '17 at 16:30
  • Can you give me specific sizes/extents? UIImage, CIImage, UIImageView - all three? –  Aug 12 '17 at 17:49

1 Answers1

0

I think you need a UIImage backed by a CGImage instead of a CIImage. Try to render your CIImage into a CGImage and create your UIImage from that CGImage. See Getting a CGImage from CIImage to know how to do this.

Xvolks
  • 2,065
  • 1
  • 21
  • 32