不旋转自动增强 UIImageView

Auto enhance UIImageView with no rotation

我已经在这两天了,希望你能帮助我。我正在尝试自动增强来自 UIImageView 的图像。下面的代码有时会旋转和调整图像大小,我很难确定何时或为什么。由于缺少关于该主题的帖子,我觉得我一定错过了一些非常简单的东西。谢谢大家!

// my UIImageView is self.imgOut 

var enhanceImg = CIImage(image: self.imgOut.image!)

let filters = enhanceImg!.autoAdjustmentFilters(options: nil)

for filter: CIFilter in filters {
  filter.setValue(enhanceImg, forKey: kCIInputImageKey)
  enhanceImg = filter.outputImage!
}

let enhancedImg = UIImage(ciImage: enhanceImg!, scale: self.imgOut.image!.scale, orientation: self.imgOut.image!.imageOrientation)
self.imgOut.image = enhancedImg

如您所见in docsautoAdjustmentFilters 得到输入 options: [CIImageAutoAdjustmentOption : Any]? = nil

所以你需要指定选项(排除level选项) level 用于定向 crop 用于缩放

let filters = enhanceImg!.autoAdjustmentFilters(options: [.level: false, .crop: false])

如果有人遇到这个 - 解决方法是使用 cgImage:

    let coreImage = CIImage(image: self.imgOut.image!)
    var enhanceImg = CIImage()
    let ciContext = CIContext(options: nil)

    let filters = coreImage!.autoAdjustmentFilters(options: [.enhance: true])

    for filter: CIFilter in filters {
        filter.setValue(coreImage, forKey: kCIInputImageKey)
        enhanceImg = filter.outputImage!
    }

    let filteredImageRef2 = ciContext.createCGImage(enhanceImg, from: enhanceImg.extent)
    self.imgOut.image = UIImage(cgImage: filteredImageRef2!, scale: self.imgOut.image!.scale, orientation: self.imgOut.image!.imageOrientation)