美文网首页
iOS压缩图片

iOS压缩图片

作者: 神一样的队友 | 来源:发表于2021-07-31 09:11 被阅读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

相关文章

网友评论

      本文标题:iOS压缩图片

      本文链接:https://www.haomeiwen.com/subject/jmphvltx.html