UIImage *image = [UIImage imageNamed:@"anyImage.png"];
NSData *data = UIImageJPEGRepresentation(image, 0.032);
UIImage *newImage = [UIImage imageWithData:data];
我发现图像的尺寸缩小但我的问题是我只需要存储100kb的图像。在我的库中,我有8159064kb的图像,通过这种方法减少到141258kb。请告诉我一种方法,将图像大小减小到100kb,无论大小如何,它都会将其转换为100kb。 提前致谢
答案 0 :(得分: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