2011-11-08 28 views
1
我使用的UIImagePickerController從照片庫帶來的圖像

保存IMG用的UIImagePickerController

,我發現這個方法保存的UIImage作爲PNG或JPEG:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

    // Create paths to output images 
    NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"]; 
    NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"]; 

    // Write a UIImage to JPEG with minimum compression (best quality) 
    // The value 'image' must be a UIImage object 
    // The value '1.0' represents image compression quality as value from 0.0 to 1.0 
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES]; 

    // Write image to PNG 
    [UIImagePNGRepresentation(<#UIImage *image#>) writeToFile:jpgPath atomically:YES]; 

    // Let's check to see if files were successfully written... 

    // Create file manager 
    NSError *error; 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 

    // Point to Document directory 
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 

    // Write out the contents of home directory to console 
    NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); 

    [picker release]; 
} 

的問題是,在該文件中PNG是6.4 MB和JPEG 3.2 MB,有一種方法可以將圖像文件保存爲較小的尺寸,然後呢?

+0

簡短的回答是肯定的可以減小尺寸。但這是一個相當複雜的過程,我現在不幸不在家,所以我不能提供更多的細節...... – Joze

回答

0

您可以調整圖像大小並以較小的尺寸保存圖像。

這裏是方法的一個例子調整圖像:

- (void)setThumbnailFromImage:(UIImage *)image { 
    CGSize origImageSize = [image size]; 

    CGRect newRect; 
    newRect.origin = CGPointZero; 
    newRect.size = CGSizeMake(40, 40); 

    // how do we scale the image? 
    float ratio = MAX(newRect.size.width/origImageSize.width, 
         newRect.size.height/origImageSize.height); 

    // create a bitmap image context 
    UIGraphicsBeginImageContext(newRect.size); 

    // Round the corners 
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect 
                cornerRadius:5.0]; 
    [path addClip]; 

    // into what rectangle shall I composite the image? 
    CGRect projectRect; 
    projectRect.size.width = ratio * origImageSize.width; 
    projectRect.size.height = ratio * origImageSize.height; 
    projectRect.origin.x = (newRect.size.width - projectRect.size.width)/2.0; 
    projectRect.origin.y = (newRect.size.height - projectRect.size.height)/2.0; 

    // draw the image on it 
    [image drawInRect:projectRect]; 

    // get the image from the image context, retain it as our thumbnail 
    UIImage *small = UIGraphicsGetImageFromCurrentImageContext(); 
    [self setThumbnail:small]; 

    // cleanup image contex resources, we're done 
    UIGraphicsEndImageContext(); 
} 

希望這將幫助你! =)

+0

這很遺憾沒有考慮在調整大小過程中可能會改變的圖像方向。並且要小心MTA,因爲這也將圍繞圖像的角落! – Joze

1

從相機返回的圖像具有1200 * 1600像素分辨率,一個像素由4個字節表示。只有減少內存大小的選項是調整圖像大小,然後將圖像壓縮爲JPEG或PNG。您可以使用下面的方法來調整圖像大小

+ (UIImage*)imageWithImage:(UIImage*)image 
       scaledToSize:(CGSize)newSize; 
{ 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

... 
0

好,最簡單的方法是將其保存爲JPEG 和降低質量。您將它設置爲1.0,這導致3.2 MB,但根據我的經驗,JPEG質量的0.3對於大多數任務已足夠 - 您甚至可以閱讀以0.3質量編寫的報紙文章的照片中的文字。大小然後是大約700K。

調整大小,你應該看看這個優秀的文章:

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

1

您可以漸漸收窄其尺寸減小圖像的大小

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
[picker dismissModalViewControllerAnimated:YES]; 

NSData *imageData = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],.5); 
UIImage *image = [UIImage imageWithData:imageData]; 
CGSize size=CGSizeMake(150,150); 
UIGraphicsBeginImageContext(size); 
[image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

imgView.image =scaledImage; // setting this image imgView"Its a image view" on my view 
相關問題