以下效果很好
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旨在逐步加载图像(如Web图像),构建快速缩略图(最大尺寸将是原始图像之一),并构建缓存或不映像的组. Here’s the manual
最简单的方法是使用UIKit,并且将使用范围(0< ratio< 1)中的缩放比例.
for example,to go from a 16×9@H_404_27@ image to a 230×180@H_404_27@ 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@H_404_27@ an specific kind of objects and this allows the
algorithm to make some needed assumptions,for example:
07002If 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
所以,您可能需要重新考虑这对您的应用程序有多重要,也可能会找到解决方法,因为现在无法实现同时兼容的质量和实时性能. 原文链接:https://www.f2er.com/iOS/336624.html