所以我正在做的是反复压缩图像,直到它适合大小框架。这需要多次尝试,放弃,然后我们告诉用户图像是大的。
我的问题是,在图像尺寸减小后,当它变为UIImage时,它的尺寸会再次恢复。
以下是缩小图像尺寸的代码:
double compressionRatio = 1;
int resizeAttempts = 5;
NSData * imgData = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
NSLog(@"Starting Size: %i", [imgData length]);
//Trying to push it below around about 0.4 meg
while ([imgData length] > 400000 && resizeAttempts > 0) {
resizeAttempts -= 1;
NSLog(@"Image was bigger than 400000 Bytes. Resizing.");
NSLog(@"%i Attempts Remaining",resizeAttempts);
//Increase the compression amount
compressionRatio = compressionRatio*0.5;
//Test size before compression
NSLog(@"Current Size: %i",[imgData length]);
imgData = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
//Test size after compression
NSLog(@"New Size: %i",[imgData length]);
}
//Set image by comprssed version
savedImage = [UIImage imageWithData:imgData];
//Check how big the image is now its been compressed and put into the UIImageView
NSData *endData = UIImageJPEGRepresentation(savedImage,1.0);
NSLog(@"Ending Size: %i", [endData length]);
好了,现在这是控制台报告:
Starting Image Size: 4076994
Image was bigger than 400000 Bytes. Resizing.
4 Attempts Remaining
Current Size: 4076994
New Compressed Size: 844482
Image was bigger than 400000 Bytes. Resizing.
3 Attempts Remaining
Current Size: 844482
New Compressed Size: 357459
Ending Image Size: 2090332
正如你所看到的那样,图像开始于4mb大。它被压缩到350kb,最后它被制成一个2mb的UIImage。
提前感谢任何能够理解这一点的人。
答案 0 :(得分:1)
我已经修改了你的功能并得到了结果,尝试一下,它将我的10MB图像减少到3MB。
-(void)compraseImage
{
UIImage *largeImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
double compressionRatio = 1;
int resizeAttempts = 5;
NSData * imgData = UIImageJPEGRepresentation(largeImage,compressionRatio);
NSLog(@"Starting Size: %i", [imgData length]);
//Trying to push it below around about 0.4 meg
while ([imgData length] > 400000 && resizeAttempts > 0) {
resizeAttempts -= 1;
NSLog(@"Image was bigger than 400000 Bytes. Resizing.");
NSLog(@"%i Attempts Remaining",resizeAttempts);
//Increase the compression amount
compressionRatio = compressionRatio*0.5;
NSLog(@"compressionRatio %f",compressionRatio);
//Test size before compression
NSLog(@"Current Size: %i",[imgData length]);
imgData = UIImageJPEGRepresentation(largeImage,compressionRatio);
//Test size after compression
NSLog(@"New Size: %i",[imgData length]);
}
//Set image by comprssed version
savedImage = [UIImage imageWithData:imgData];
//Check how big the image is now its been compressed and put into the UIImageView
// *** I made Change here, you were again storing it with Highest Resolution ***
NSData *endData = UIImageJPEGRepresentation(largeImage,compressionRatio);
NSLog(@"Ending Size: %i", [endData length]);
NSString *path = [self createPath:@"myImage.jpg"];
NSLog(@"%@",path);
[endData writeToFile:path atomically:YES];
}
-(NSString *) createPath:(NSString *)withFileName
{
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:withFileName];
return path;
}