Der*_*unt 5 uiimageview uiimage ios ciimage swift
我正在尝试将a的图像属性设置为UIImageView我正在模糊的图像CoreImage.代码与未过滤的图像完美配合,但是当我将背景图像设置为过滤后的图像时,contentMode似乎停止工作UIImageView- 而不是填充方面,图像将垂直拉伸.除了设置contentMode代码,我还在故事板上设置它,但结果是相同的.
我正在使用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
}
Run Code Online (Sandbox Code Playgroud)
为什么过滤CIImage导致我的图像被忽略contentMode,我该如何解决这个问题?
解决方案是更换您的线路:
let blurredImage = UIImage(CIImage: croppedImage)
Run Code Online (Sandbox Code Playgroud)
有这两行:
let context = CIContext(options: nil)
let blurredImage = UIImage (CGImage: context.createCGImage(croppedImage, fromRect: croppedImage.extent))
Run Code Online (Sandbox Code Playgroud)
所以你的完整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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
427 次 |
| 最近记录: |