大佬教程收集整理的这篇文章主要介绍了ios – 为什么这段代码比天真的方法更好地解压缩UIImage?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
+ (UIImage *)decompressedImageFromImage:(UIImage *)image { UIGraphicsBeginImageContextWithOptions(image.size,YES,0); [image drawAtPoint:CGPointZero]; UIImage *decompressedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return decompressedImage; }
但是,我仍然有很长的加载时间,UI口吃和很多内存压力.我刚发现another solution:
+ (UIImage *)decodedImageWithImage:(UIImage *)image { CGImageRef imageRef = image.CGImage; // System only supports RGB,set explicitly and prevent context error // if the downloaded image is not the supported format CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL,CGImageGetWidth(imageRef),CGImageGetHeight(imageRef),8,// width * 4 will be enough because are in ARGB format,don't read from the image CGImageGetWidth(imageRef) * 4,colorSpace,// kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little // makes system don't need to do extra conversion when displayed. kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32LittlE); CGColorSpaceRelease(colorSpacE); if ( ! context) { return nil; } CGRect rect = (CGRect){CGPointZero,CGImageGetHeight(imageRef)}; CGContextDrawImage(context,rect,imageRef); CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context); CGContextRelease(context); UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef]; CGImageRelease(decompressedImageRef); return decompressedImage; }
此代码的数量级更好.图像几乎立即加载,没有UI断断续续,内存使用率已经下降.
所以我的问题是双重的:
>为什么第二种方法比第一种方法好得多?
>如果第二种方法由于设备的独特参数而更好,是否有办法确保它对所有iOS设备(现在和未来)同样有效?我不想假设我的原生位图格式发生了变化,重新引入了这个问题.
以上是大佬教程为你收集整理的ios – 为什么这段代码比天真的方法更好地解压缩UIImage?全部内容,希望文章能够帮你解决ios – 为什么这段代码比天真的方法更好地解压缩UIImage?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。