【问题标题】:If a filter is applied to a PNG where height > width, it rotates the image 90 degrees. How can I efficiently prevent this?如果将过滤器应用于高度 > 宽度的 PNG,它会将图像旋转 90 度。我怎样才能有效地防止这种情况?
【发布时间】:2017-09-17 14:16:34
【问题描述】:

我正在制作一个简单的过滤器应用程序。我发现,如果您从相机胶卷中加载一个 PNG 图像(PNG 没有方向数据标志)并且高度大于宽度,则在对所述图像应用某些失真过滤器时,它将旋转并呈现它自己就好像它是一幅风景图像一样。

我在打开的许多选项卡中的某处在线找到了以下技术,它似乎完全符合我的要求。它使用第一次加载时图像的原始比例和方向。

let newImage = UIImage(CIImage:(output), scale: 1.0, orientation: self.origImage.imageOrientation)

但这是我尝试使用它时收到的警告:

Ambiguous use of 'init(CIImage:scale:orientation:)'

这是我正在努力工作的全部内容:

//global variables    
 var image: UIImage!
 var origImage: UIImage!


func setFilter(action: UIAlertAction) {


origImage = image

// make sure we have a valid image before continuing!

    guard let image = self.imageView.image?.cgImage else { return }

    let openGLContext = EAGLContext(api: .openGLES3)
    let context = CIContext(eaglContext: openGLContext!)
    let ciImage = CIImage(cgImage: image)

let currentFilter = CIFilter(name: "CIBumpDistortion")
currentFilter?.setValue(ciImage, forKey: kCIInputImageKey)

if let output = currentFilter?.value(forKey: kCIOutputImageKey) as? CIImage{

//the line below is the one giving me errors which i thought would work.
    let newImage = UIImage(CIImage:(output), scale: 1.0, orientation: self.image.imageOrientation)
        self.imageView.image = UIImage(cgImage: context.createCGImage(newImage, from: output.extent)!)}

过滤器都可以工作,不幸的是,出于我怀疑的原因,它们将上述图像旋转了 90 度。

我尝试了一些其他方法,例如使用检查 UIimages 方向的扩展程序并将 CIimage 转换为 Uiimage,使用扩展程序,然后尝试将其转换回 Ciimage 或仅将 UIimage 加载到 imageView 以进行输出.我在这个过程中遇到了一个又一个的障碍。我开始似乎真的很费解,只是为了让某些图像也恢复到它们的默认方向。

任何建议将不胜感激!

编辑:这里是我尝试的方法:When applying a filter to a UIImage the result is upside down

【问题讨论】:

  • 我个人喜欢并使用过这个答案:stackoverflow.com/a/41247037/6855532 扩展是一种干净的方法。
  • 谢谢,我尝试将我的 CIimage 转换为 UIimage,如下所示:让 image = UIImage(CIImage: ciimage) 使用您提供的扩展名,现在转换让我对 'init 的使用模棱两可(CIImage:) 错误'。关于如何正确将 Ciimage 转换为 uiimage 的任何想法?
  • 谢谢,我还需要帮助将我正在使用的 CIImage 转换为 uiimage 以使用它。有什么建议吗?
  • 我忘了这个你可以使用 CIImage 图像属性扩展来将你的气候渲染成一个可用的 UIImage stackoverflow.com/a/43011878/2303865

标签: ios swift swift3


【解决方案1】:

我找到了答案。我最大的问题是“'init(CIImage:scale:orientation:)' 的模糊使用”

事实证明,Xcode 自动将代码填充为“CIImage:scale:orientation”,而它应该是 ciImage:scale:orientation' 这个非常模糊的错误让一个新开发人员像我一样为此挠头 3 天. (CGImage 和 UIImage 初始化也是如此,但我最初的错误是与 CIImage 相关的,所以我用它来解释。)

凭借这些知识,我能够为我的新输出制定以下代码:

if let output = currentFilter?.value(forKey: kCIOutputImageKey) as? CIImage{

    let outputImage = UIImage(cgImage: context.createCGImage(output, from: output.extent)!)

    let imageTurned = UIImage(cgImage: outputImage.cgImage!, scale: CGFloat(1.0), orientation: origImage.imageOrientation)
    centerScrollViewContents()
    self.imageView.image = imageTurned
    }

此代码替换 OP 中的 if let 输出。

【讨论】:

    猜你喜欢
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多