0
UIImage *image = [UIImage imageNamed:@"anyImage.png"];

NSData *data = UIImageJPEGRepresentation(image, 0.032);

UIImage *newImage = [UIImage imageWithData:data];

我发现这种图像尺寸减小了,但我的问题是我只需要存储 100kb 的图像。在我的库中,我有 8159064kb 的图像,通过这种方法可以减少到 141258kb。请告诉我一种将图像大小减小到 100kb 的方法,无论大小将其转换为 100kb。提前致谢

4

1 回答 1

2

你应该调整它的大小,image.size这样它就可以将你的大图像缩小到 100k

你可以做一个UIImage类别

UIImage(Resize)

+(UIImage*)imageWithImage:(UIImage*)image andWidth:(CGFloat)width andHeight:(CGFloat)height
{
    UIGraphicsBeginImageContext( CGSizeMake(width, height));
    [image drawInRect:CGRectMake(0,0,width,height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

然后while循环调整图像大小以适应 <= 100KB

NSData *data = UIImagePNGRepresentation(_yourImage);
while (data.length / 1000 >= 100) {
    _yourImage = [UIImage imageWithImage:_yourImage andWidth:image.size.width/2 andHeight:image.size.height/2];
    data = UIImagePNGRepresentation(_yourImage);
}

// _yourImage is now reduce the size which is <= 100KB
于 2013-03-19T12:33:02.723 回答