项目中遇到一个问题就是,图片太大,导致上传到服务器或者下载时使用 SDWebImage(最大允许 2M 图片)时 闪退。
解决办法是在本地先处理好图片。按照指定宽度等比例 缩放图片后,再使用。
//使用UIImage *portraitImg = [info objectForKey:UIImagePickerControllerOriginalImage]; portraitImg = [Util reSizeImage:portraitImg WithWidth:1242];
//代码 //对image按照width缩放剪裁 + (UIImage *)reSizeImage:(UIImage *)originImage WithWidth:(float)width{ float scaleSize = width/originImage.size.width; if (scaleSize < 1) { UIGraphicsBeginImageContext(CGSizeMake(originImage.size.width * scaleSize, originImage.size.height * scaleSize)); [originImage drawInRect:CGRectMake(0, 0, originImage.size.width * scaleSize, originImage.size.height * scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } return originImage; }
时间: 2024-10-10 16:37:35