使用UIImagePickerController保存img

时间:2011-11-08 12:00:44

标签: iphone objective-c

我正在使用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,有一种方法可以将图像文件保存为更小的尺寸吗?

4 个答案:

答案 0 :(得分: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;
}

...

答案 1 :(得分: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

答案 2 :(得分: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();
}

希望这会对你有所帮助! =)

答案 3 :(得分:0)

嗯,最简单的方法是将其保存为 JPEG并降低质量。你将它设置为1.0,结果是3.2 MB,但根据我的经验, 0.3 的JPEG质量足以满足大多数任务 - 你甚至可以阅读用0.3写的报纸文章照片中的文字质量。那么大小约为700K。

要调整大小,您应该看一下这篇优秀的文章:

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