ios - 如何在 ImageIO iOS 框架中升级图像?

标签 ios swift

我经历了逐步发现 iOS 中性能最佳的图像大小调整解决方案的常规过程,并最终找到了 ImageIO。

以下效果很好

if let currentImage = CGImageSourceCreateWithData(imageData, nil)

   //...some code that extracts the existing size, and based on my maximum expected horizontal size..calculates a new horizontal/vertical maximum size..

let options: CFDictionary = [kCGImageSourceThumbnailMaxPixelSize as NSString            : maximumImageWidthOrHeight,
                             kCGImageSourceCreateThumbnailFromImageAlways as NSString   : true]

CGImageSourceCreateThumbnailAtIndex(image, options)

但仅用于下采样。<​​/p>

如果我有全高清图像,即。 1920 x 1080,调整大小(保持纵横比),使其水平适合 320 像素,因为这是我设置的最大水平尺寸。

但是,如果我传入一个 16 像素 x 9 像素的图像……一个非常小的图像(显然具有 16:9 的纵横比)。此图像未调整大小。

我想将其调整为 320 x 180 像素。

在 imageIO 框架中是否有选项的组合/错过的方法调用/任何可以给我的东西?

最佳答案

简短的回答是您不能使用 ImageIO 来做到这一点。

ImageIO 旨在逐步加载图像(如在网络图像中)、构建快速 缩略图(< em>哪个最大尺寸将是原始图像),并构建缓存图像组。 Here's the manual

最简单的方法是使用 UIKit,并使用 (0 < ratio < 1) 范围内的比例。

for example, to go from a 16x9 image to a 230x180 will be:

 let image = UIImage(data: yourImageData!, scale: 0.050)!

还有其他方法,如 NSHipster 中所述这只是有点复杂,但可能值得研究,并进行一些性能测试以选择哪个更好的上采样。但这取决于您的测试。

无论哪种方式,结果都会很糟糕,因为上采样 仍在研究中,是一项非常艰巨的任务。

There are several papers on this topic, but they are usually related to upsampling an specific kind of objects and this allows the algorithm to make some needed assumptions, for example: Hallucinating Faces: TensorPatch Super-Resolution and Coupled Residue Compensation

If I were to choose a way to achieve this and quality were my goal, I would go for OpenCV algorithms on superresolution.

But, they are iterative algorithms, so maybe don't fit your performance needs.

Here's a demostration of how it works with the popular image "Lena"

因此,您可能需要重新考虑这对您的应用有多重要,并且可能会找到一个解决方法,因为没有办法同时实现质量和接近实时的性能现在 . ;-)

编码愉快!

关于ios - 如何在 ImageIO iOS 框架中升级图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31942936/

相关文章:

ios - 如何在swift中实现监听器模式

swift - Realm 类名称与数据库中的存储名称不同

iphone - 在 hittest 中的点上应用 CGAffineTransformRotate

ios - 使用自动布局将表格 View 单元格高度调整为图像的高度

swift - Swift 中 Storyboard 中的 "No @implementation found for the class"错误

ios - SKTextureAtlas 和 3x 图像

ios - 添加 subview Controller 后 UIView 容器消失

ios - 增加 slider 宽度 : swift

ios - xcode根据文件模板从物理文件夹创建组

ios - 横向模式下的 UIImagePickerViewController