压缩从设备或从图书馆,iOSselect的图像

在我的应用程序中,用户可以从设备拍摄照片或从图库中上传照片,将其设置为他的个人资料照片。 现在当用户完成后,我需要将该图像上传到服务器。 通常来自设备的图像大小为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?

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

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

 - (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]; } 

这一定会解决你的问题与图像使这种常见的方法在多个视图中使用…

这是一个替代答案,我认为它更简洁。 这是从堆栈溢出的另一个答案,但一直在我的代码。 我得到〜15kB大小的图像,这是从4s(2.5k×3.5k像素)到更有用的分辨率(400×600)的相机拍摄的标准图像的resize。 然后压缩UIImageJPEGRepresentation(图片,0.1)。

 CGRect rect = CGRectMake(0,0,capturedImage.size.width/6,capturedImage.size.height/6); UIGraphicsBeginImageContext( rect.size ); [capturedImage drawInRect:rect]; UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData *imageDataForResize = UIImageJPEGRepresentation(picture1,0.1); UIImage *img=[UIImage imageWithData:imageDataForResize]; capturedImage = img; // reassigning to original image as to prevent changing things later in the code 

谢谢! 我希望这可以帮助别人! 当您尝试执行从服务器加载图像的QUICK集合视图加载时,此问题很重要。

迅速:

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { var capturedImage = info[UIImagePickerControllerEditedImage] as! UIImage let rect = CGRectMake(0, 0, capturedImage.size.width/6, capturedImage.size.height/6) UIGraphicsBeginImageContext(rect.size) capturedImage.drawInRect(rect) let resizedImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() let compressedImageData = UIImageJPEGRepresentation(resizedImage, 0.1) capturedImage = UIImage(data: compressedImageData)! }