iOS压缩图片
2021-07-31 本文已影响0人
神一样的队友
使用SDWebImage加载较大图片会导致内存过大闪退
一张图片2.4MB,根据搜索出的方法尝试都会出现闪退,直到看到一篇文章,理论是通过改变图片尺寸压缩图片
//压缩图片
+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();
// Return the new image.
return newImage;
}
参考文章:https://www.jianshu.com/p/8150a8e7c0e4