有时候从App上传图片给后台,由于图片较大, 需要将图片压缩一下。
压缩的时候注意有的方案会卡线程,耗时。
这里有个方案:不耗时,不卡线程
-(UIImage *)makeThumbnailFromImage:(UIImage *)srcImage scale:(double)imageScale{ UIImage *thumbail = nil; CGSize imageSize = CGSizeMake(srcImage.size.width*imageScale, srcImage.size.height*imageScale); if (srcImage.size.width != imageSize.width || srcImage.size.height != imageSize.height) { UIGraphicsBeginImageContext(imageSize); CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height); [srcImage drawInRect:imageRect]; thumbail = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } else{ thumbail = srcImage; } return thumbail; }
参考自:https://www.cnblogs.com/ChouDanDan/p/5038396.html#commentform