【问题标题】:set type of an image captured using iPhone camera设置使用 iPhone 相机拍摄的图像的类型
【发布时间】:2013-02-27 19:21:36
【问题描述】:

如果我们使用 iPhone 相机拍摄图片,图片会默认保存为 JPEG 格式。

我想以其他格式(如 PNG)保存捕获的图像。有可能吗?

当我们从应用程序调用 iPhone 相机时,是否可以通过代码执行此操作,我们可以设置捕获图片后必须保存的图像类型吗?我的意思是在拍照前打开Camera并设置类型?

【问题讨论】:

    标签: iphone ios objective-c camera uiimagepickercontroller


    【解决方案1】:
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    // png image data
    
    NSData *pngData = UIImagePNGRepresentation(image);
    
    // jpeg image data
    
    NSData *jpegData = UIImageJPEGRepresentation(image, compressionQuality);  // compressionQuality = 0.0 to 1.0 --> 0 means maximum compression and 1 means minimum compression
    

    获得 NSData 后,您可以将此数据保存到特定文件中。更多详情请通过link

    希望这会对你有所帮助。

    【讨论】:

    • 检查更新的答案。对于 JPEG 数据,需要压缩比。有关更多详细信息,请访问 ans 中附加的苹果参考链接。
    • UIImagePickerControllerOriginalImage 提供了已经保存的图像。我需要从相机中捕获具有 PNG 格式类型的图像
    • UIImagePickerControllerOriginalImage - 它将以对象格式 UIImage 为您提供图像,该图像尚未存储在任何地方。要保存该图像,您需要 writeToFile。在此之前,您可以使用给定的两种方法将 UIImage 转换为 NSData。这些方法将您的 UIImage 对象转换为特定的图像类型 (PNG/Jpeg) 数据。
    • 获得 NSData 格式后,使用 writeToFile 方法保存文件。根据您选择的转换(PNG/Jpeg)提供文件名+扩展名
    【解决方案2】:

    这将帮助您将图像保存为 png 类型

    -(void)imagePickerController:(UIImagePickerController *)picker
      didFinishPickingImage : (UIImage *)image
                 editingInfo:(NSDictionary *)editingInfo
    {
    NSError *error;
    
    NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@.png",@"photoname"]];
    [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:NO];
    
    
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    NSLog(@"Documents directory: %@", [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error]);
    
    [self dismissModalViewControllerAnimated:YES];
    }
    

    【讨论】:

    • 我们可以使用上面的 PNG 或 JPEG 表示形式保存为 PNG 或 JPEG。但我想知道的是,第一次保存应该是 PNG 格式..我对转换不感兴趣..
    • @jithen,上面代码中没有图片格式的转换,直接将图片保存为png格式
    • 当用户在iphone相机中拍照时,会调用didFinishPickingImage,直接将图片保存为png格式,见代码
    【解决方案3】:

    您可以在方法中使用与此类似的内容将捕获的对象保存为 PNG:

    // Create paths to output images
    NSString  *thePngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Example.png"];
    NSString  *theJpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Example.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:theJpgPath atomically:YES];
    
    // Write image to PNG
    [UIImagePNGRepresentation(image) writeToFile:thePngPath atomically:YES];
    

    【讨论】:

    • 如果我们有图像,那么我们可以使用上面的代码转换为PNG或JPEG格式......但我的疑问是如何直接从相机键捕获PNG格式的图像......我们捕获后可以转换..但我想要直接以PNG格式的图像而不需要任何转换...
    【解决方案4】:

    使用此代码,

    NSData* pngdata = UIImagePNGRepresentation (image); //PNG wrap 
    UIImage* img = [UIImage imageWithData:pngdata];
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
    

    【讨论】:

    • 这里“图像”的格式类型是什么......如果你从相机捕捉到它,它肯定是JPEG......我想知道的是,我们可以使用PNG格式捕捉图像吗相机?如果是,该怎么做
    猜你喜欢
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多