【问题标题】:Swift: UIImage orientation problem when applying filter in swiftSwift:在swift中应用过滤器时的UIImage方向问题
【发布时间】:2020-12-11 21:59:12
【问题描述】:

我想通过CIPhotoEffectNoir把一张图片转成灰度图,但是用了之后,图片旋转了。我搜索了很多,但答案无法解决我的问题。

这是我的代码:

    func grayscale(image: UIImage) -> UIImage? {
        let context = CIContext(options: nil)
        if let filter = CIFilter(name: "CIPhotoEffectNoir") {
            filter.setValue(CIImage(image: image), forKey: kCIInputImageKey)
            
            if let output = filter.outputImage {
                if let cgImage = context.createCGImage(output, from: output.extent) {
                    return UIImage(cgImage: cgImage)
                }
            }
        }
        return nil
    }

之前:

之后:

我想要什么:

【问题讨论】:

    标签: ios swift xcode uiimage uiimageorientation


    【解决方案1】:

    当您创建新的 UIImage 实例时,不要忘记使用原始 UIImage 中的 scaleorientaion 值。

    return UIImage(
      cgImage: cgimg, 
      scale: image.scale, 
      orientation: image.imageOrientation)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 2016-08-24
      • 2017-01-11
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      相关资源
      最近更新 更多