添加过滤器时,照片会被拉伸

时间:2018-02-18 11:37:00

标签: ios swift imageview ciimage

添加过滤器时,照片会被拉伸,并且不会响应aspectFit

    let photoProcess = CIFilter(name: "CIPhotoEffectProcess")

    let image = UIImage(named: "tetka")

    let ciImage = CIImage(image: image!)

    let test = photoProcess?.outputImage

    let img = UIImage(ciImage: test!)

    photoImageView.image = img
    photoImageView.contentMode = .scaleAspectFit

enter image description here

1 个答案:

答案 0 :(得分:3)

尝试使用此代码

    let inputImage = UIImage(named: "blueDog.png")!
    let ciContext = CIContext(options: nil)
    let coreImage = CIImage(image: inputImage)
    let filter = CIFilter(name: "CIPhotoEffectProcess" )
    filter!.setDefaults()
    filter!.setValue(coreImage, forKey: kCIInputImageKey)
    let filteredImageData = filter!.value(forKey: kCIOutputImageKey) as! CIImage
    let filteredImageRef = ciContext.createCGImage(filteredImageData, from: filteredImageData.extent)
    let imageForUser = UIImage(cgImage: filteredImageRef!);

    photoImgVw.image = imageForUser 

您可以从此link

获取更多信息

<强>输出

enter image description here