UIImagePicker图片大小超过原始版本

时间:2014-08-22 12:33:35

标签: ios compression ios-simulator uiimagepickercontroller jpeg

WorkGround

我在模拟器中有一个大小为53kb的图像(.JPG格式)。

现在我使用UIImagePickerController选择此图像并将其保存到本地。

选择图像并将其存储在本地的代码如下:

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

NSString *mediaType = info[UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    // Media is an image

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (image) {
        NSData *jpegData = UIImageJPEGRepresentation(image, 1);

    // Store image.
        BOOL isImageSave = [jpegData writeToFile:filePath atomically:NO];

        if (isImageSave) {
            DLog(@"Image saved");
        } else {
            DLog(@"Error");
        }
    }
}
[self.view dismissViewControllerAnimated:YES completion:nil];

}

问题:

此处原始图像为53kb但是当我选择此图像时,图像尺寸将增加并且存储图像的尺寸为142kb。在这里,我使用压缩来减小尺寸。但我在UIImagePickerController委托方法中检查两次,它返回更大尺寸的图像。

有没有人有想法,为什么选择更大的图像。有没有办法在尺寸方面获得原始尺寸的图像?

2 个答案:

答案 0 :(得分:1)

您可以在特定帧中调整图像视图的大小。使用下面的代码

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

   return newImage;
}

然后将此方法称为:

UIImage *myIcon = [self imageWithImage:myUIImageInstance scaledToSize:CGSizeMake(20, 20)];

就图像的存储而言,与iPhone一起使用的最快图像格式是PNG,因为它对该格式进行了优化。但是,如果要将这些图像存储为JPEG,则可以使用UIImage并执行以下操作:

NSData *dataForJPEGFile = UIImageJPEGRepresentation(theImage, 0.6);

答案 1 :(得分:0)

您使用的JPEG编码质量为1,相当于100%质量。执行JPEG编码时尝试使用0.2-0.8的设置,然后检查文件大小。

相关问题