使用CIFilter将滤镜应用于UIImage并旋转图像

时间:2015-12-27 19:44:40

标签: ios objective-c swift cifilter

当我应用于UIImage过滤器时,图像的结果是旋转了!我试过这个:

var filterNames: [String] = ["CIPhotoEffectTonal","CIPhotoEffectNoir","CIMaximumComponent","CIMinimumComponent","CIDotScreen", "CISepiaTone", "CIFalseColor", "CIColorInvert", "CIColorPosterize", "CIPhotoEffectChrome", "CIPhotoEffectInstant"]

    let CIfilterName = filterNames[indexPath.row]
    print(CIfilterName)

    let ciContext = CIContext(options: nil)
    let startImage = CIImage(image: originalImage)

    let filter = CIFilter(name: CIfilterName)
    filter!.setDefaults()

    filter!.setValue(startImage, forKey: kCIInputImageKey)
    let filteredImageData = filter!.valueForKey(kCIOutputImageKey) as! CIImage
    let filteredImageRef = ciContext.createCGImage(filteredImageData, fromRect: filteredImageData.extent)

    let originalOrientation: UIImageOrientation = (imageView.image?.imageOrientation)!
    let originalScale: CGFloat = (imageView.image?.scale)!
    let newPtImage: UIImage = UIImage.imageWithCGImage(filteredImageRef, scale: originalScale, orientation: originalOrientation)
    imageView.image = newPtImage

    imageView.image = UIImage(CGImage: filteredImageRef);

规模的最后一部分是我尝试过但我展示了部分代码,这样你就可以看到我做错了什么或者我不知道是什么。

我找到了这个答案,但它在Objective-c中:Stackoverflow answer in obj-c

2 个答案:

答案 0 :(得分:2)

您可以尝试使用UIImageOrientation来解决此问题,但我绝对推荐带有扩展名的switchGithub Sample)语句,因为稍后当照片上传到服务器时也可能会再次改变它的方向。

实施例

UIImage(CGImage: imageRef, scale: 1.0, orientation: UIImageOrientation.Right)

答案 1 :(得分:0)

当图像高度大于图像宽度时会出现此问题,因此如果图像的高度大于宽度,则应在应用CIFilter之前通过相互交换图像高度和宽度来调整图像大小....