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

我经历了在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)

但仅用于下采样.

如果我有一个全高清的图像即1920 x 1080,它被调整大小(保持宽高比),使其适合320 px水平,因为这是我设置为最大水平尺寸.

但是,如果我传递一个16像素×9像素的图像,那么一个很小的图像(并且具有16:9的宽高比).此图像未调整大小.

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

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

解决方法

简单的答案是你不能使用ImageIO来做.

ImageIO旨在逐步加载图像(如Web图像),构建快速缩略图(最大尺寸将是原始图像之一),并构建缓存或不映像的组. Here’s the manual

最简单的方法是使用UIKit,并且将使用范围(0< ratio< 1)中的缩放比例.

for example,to go from a 16×9 image to a 230×180 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:
07002

If I were to choose a way to achieve this and quality were my goal,I would go for 07003 07004.

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

07005

所以,您可能需要重新考虑这对您的应用程序有多重要,也可能会找到解决方法,因为现在无法实现同时兼容的质量和实时性能.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...