图像未在ios中压缩

时间:2016-10-03 08:05:51

标签: ios objective-c uiimage

图像未压缩我使用以下代码

UIImage *image = [UIImage imageNamed:@"1.png"];
CGSize newSize = CGSizeMake(100.0f, 100.0f);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_backgroundImageView.image = newImage;

1 个答案:

答案 0 :(得分:0)

如果要压缩图像

$("form > input")

您可以使用以下代码

压缩所需的图片尺寸
UIImage *img = [UIImage imageNamed:@"yourimagename"];
NSData *postData = UIImageJPEGRepresentation(img, 1.0);
NSInteger imgSizeBytes = [postData length];
double imgSizeKBytes = ceil((double)imgSizeBytes / 1024);
NSString *strBytes;
if(imgSizeKBytes > 1024) {
    double imgSizeMBytes = (double)imgSizeKBytes / 1024;
    strBytes = [NSString stringWithFormat:@"%.1f MB", imgSizeMBytes];
}
else {
    strBytes = [NSString stringWithFormat:@"%d KB", (int)imgSizeKBytes];
}

imageView.image = [UIImage imageWithData:postData scale:0.1];

上面我给了75.如果你想给你的尺码,那就去吧。