2

如果我们使用 iPhone 相机拍摄照片,图像将默认以 JPEG 格式保存。

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

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

4

4 回答 4

6
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 后,您可以将此数据保存到特定文件。有关更多详细信息,请访问此链接

希望这会帮助你。

于 2013-03-12T10:24:46.517 回答
5

这将帮助您将图像保存为 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];
}
于 2013-03-12T10:23:20.593 回答
1

您可以在方法中使用与此类似的方法将捕获的对象保存为 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];
于 2013-03-12T10:22:42.920 回答
1

使用此代码,

NSData* pngdata = UIImagePNGRepresentation (image); //PNG wrap 
UIImage* img = [UIImage imageWithData:pngdata];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
于 2013-03-12T10:22:45.490 回答