优化从相机胶卷中拾取的图像以减小文件大小

时间:2013-05-09 11:01:31

标签: ios objective-c cocoa-touch

在我的应用程序中,如果用户从相机胶卷中选择一张图像作为compnay徽标(添加到最终的pdf),它可以将附加文件大小从8mb(没有图像)到29mb。当用户通过电子邮件发送文件比大多数服务器允许附件大小

时,这是一个问题
- (IBAction)schemeLogoPressed:(id)sender {
 LogCmd();
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = NO;
    [self.editController presentModalViewController:imagePicker animated:YES];
   }    

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
 {
    DebugLog(@"info dict: %@", info);
    [picker dismissModalViewControllerAnimated:YES];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.schemeLogo.backgroundColor = [UIColor whiteColor];
    self.schemeLogo.image = image;
    NSData *imageData1 = UIImagePNGRepresentation(image);
    NSString *path1 = [ICUtils pathForDocument:@"schemeLogo.png"];
    [imageData1 writeToFile:path1 atomically:NO];

我可以做些什么来优化拾取的图像尺寸?

1 个答案:

答案 0 :(得分:5)

更改此行:

NSData *imageData1 = UIImagePNGRepresentation(image);

为:

NSData *imageData1 = UIImageJPEGRepresentation(image, 0.9f);

第二个参数(0.9浮点数)是质量。数量越多,质量越高。从0.0到1.0