在我的应用程序中,允许用户从设备拍摄照片或从库上传照片以将其设置为他的个人资料照片。现在当用户完成后,我需要将该图像上传到服务器。通常从设备拍摄的图像大小为 5-6MB。在上传到服务器之前,我需要将其压缩到 25KB。所以我使用以下方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    [picker release];
    profilePicImageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    NSData* pngData = UIImagePNGRepresentation(profilePicImageView.image);
    NSLog(@"image size of png is %d", [pngData length]);
    NSData *imageData = UIImageJPEGRepresentation(profilePicImageView.image, 0.00001);
    NSLog(@"image size is %d", [imageData length]);
}

这种方法的问题是,无论我设置的缩放参数有多小,压缩文件的大小都不会低于~230KB,这比我需要的要高出近 10 倍。

上述方法有什么问题?我不能将图像压缩到 25KB 吗?

任何帮助,建议将不胜感激。

最佳答案

我认为下面的代码可以帮助您做您想做的事,请在您的应用程序中使用之前删除我的一些代码。

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  [appDelegate.objList setHidden:FALSE];
  [self dismissModalViewControllerAnimated:YES];

UIImage *capturedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[self performSelector:@selector(waitUntillImageCaptured:) withObject:capturedImage afterDelay:0.2];
}

-(void)waitUntillImageCaptured:(UIImage *)originalImage
{
UIImage *tempimg = originalImage;
//UIImageWriteToSavedPhotosAlbum(tempimg,nil,nil,nil);

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

CGSize newSize = CGSizeMake(320, 480);
UIGraphicsBeginImageContext(newSize);

[tempimg drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();

NSString *strUser = [NSString stringWithFormat:@"%@",[imgDictName objectForKey:@"UsersName"]];
strUser = [strUser stringByReplacingOccurrencesOfString:@" " withString:@""];
[strUser retain];
NSString * strUserLocal = [strUser stringByAppendingString:@"temp.png"];

NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:strUserLocal];
UIImageView *tempView = [[UIImageView alloc] initWithImage:newImage];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(tempView.image)];
[tempView release];

if(data != nil) {
    [data writeToFile:imagePath atomically:YES];
}
else{
    [[NSFileManager defaultManager] removeItemAtPath:imagePath error:NULL];
}

[pool release];

[self performSelector:@selector(thumbWithSideOfLength:) withObject:strUser afterDelay:0.3];
}

- (void)thumbWithSideOfLength:(NSString*)strImgName
{
[self onEditing];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullPathToThumbImage = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@temp.png", strImgName]];

UIImage *thumbnail;

UIImage *mainImage = [UIImage imageWithContentsOfFile:fullPathToThumbImage];

UIImageView *mainImageView = [[UIImageView alloc] initWithImage:mainImage];

CGRect clippedRect = CGRectMake(0, 0, mainImage.size.width,  mainImage.size.height);

CGFloat scaleFactor = 0.8;

UIGraphicsBeginImageContext(CGSizeMake(mainImage.size.width * scaleFactor, mainImage.size.height * scaleFactor));
CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextClipToRect(currentContext, clippedRect);

CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);

[mainImageView.layer renderInContext:currentContext];

thumbnail = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(thumbnail);

[imageData writeToFile:fullPathToThumbImage atomically:YES];
userImageView.backgroundColor = [UIColor clearColor];
[userImageView imageFromImagePath:fullPathToThumbImage];

[mainImageView release];
}

这肯定会解决您的图像问题,使这种在多个 View 中使用的常用方法......

关于ios - 压缩从设备拍摄或从库中选择的图像,iOS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9972454/

10-11 13:04