关于聊天图片的压与缩

今天来公司加班,准备搞一下聊天时发送拍照图片慢的问题,因为之前也做了一些对图片的处理,但是发送的过程中存在延迟。之前只是对图片进行了压的动作,通过判断data.length的长度,来进行压.


-(NSData)handleImage:(UIImage)originImage{

NSData *data = UIImageJPEGRepresentation(originImage, 1);
NSData *newData = nil;
CGFloat compression = 0.9f;

if (data.length

}


后来通过百度才发现如果想要压缩到更小,就要对图片自身进行裁剪,于是才想到这个方法
+ (UIImage *)compressImage:(UIImage *)image newWidth:(CGFloat)newImageWidth
{
if (!image) return nil;
float imageWidth = image.size.width;
float imageHeight = image.size.height;
float width = newImageWidth;
float height = image.size.height/(image.size.width/width);

float widthScale = imageWidth /width;
float heightScale = imageHeight /height;
UIGraphicsBeginImageContext(CGSizeMake(width, height));

if (widthScale > heightScale) {
    [image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];
}
else {
    [image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];
}
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;

}

通过这两个方法的处理,在进行图片发送就变得很快了。

你可能感兴趣的:(关于聊天图片的压与缩)