一张700x700的png图像,显示的图像视图成本为1.88Mib内存,但如果我用UIImageJPEGRepresentation(数据,0.9)压缩它,内存成本降低到736Kib,但图像大小保持不变,jpg像素没有alpha,但内存成本降低了近62%,我想知道UIImageJPEGRepresentation()是如何工作的?thx
加载png图像:
NSString *path = [[NSBundle mainBundle] pathForResource:@"bbb" ofType:@"png"];
NSData * data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:NULL];
UIImage * img = [UIImage imageWithData:data];加载jpg图像
NSString *path = [[NSBundle mainBundle] pathForResource:@"bbb" ofType:@"png"];
NSData * data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:NULL];
UIImage * img = [UIImage imageWithData:data];
img = [UIImage imageWithData:UIImageJPEGRepresentation(img, 0.9)];jpg内存成本:

png内存成本:

发布于 2020-04-20 21:25:51
UIImage imageWithData:将对PNG文件进行解压缩。结果大小与分辨率匹配:
700像素×700像素×4字节/像素= 1,960,000 = 1.87 MByte
此图像不再与PNG格式和压缩技术相关。
第二个示例首先解压缩PNG文件,然后使用JPEG算法对其进行压缩。结果是-当然是-更小,因为它被压缩了。
JPEG和PNG都是用于压缩图像的算法。你会在网上找到很多关于它们是如何工作的文档。这绝不是微不足道的。
https://stackoverflow.com/questions/61317406
复制相似问题