【发布时间】:2013-02-27 19:21:36
【问题描述】:
如果我们使用 iPhone 相机拍摄图片,图片会默认保存为 JPEG 格式。
我想以其他格式(如 PNG)保存捕获的图像。有可能吗?
当我们从应用程序调用 iPhone 相机时,是否可以通过代码执行此操作,我们可以设置捕获图片后必须保存的图像类型吗?我的意思是在拍照前打开Camera并设置类型?
【问题讨论】:
标签: iphone ios objective-c camera uiimagepickercontroller
如果我们使用 iPhone 相机拍摄图片,图片会默认保存为 JPEG 格式。
我想以其他格式(如 PNG)保存捕获的图像。有可能吗?
当我们从应用程序调用 iPhone 相机时,是否可以通过代码执行此操作,我们可以设置捕获图片后必须保存的图像类型吗?我的意思是在拍照前打开Camera并设置类型?
【问题讨论】:
标签: iphone ios objective-c camera uiimagepickercontroller
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
希望这会对你有所帮助。
【讨论】:
这将帮助您将图像保存为 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];
}
【讨论】:
didFinishPickingImage,直接将图片保存为png格式,见代码
您可以在方法中使用与此类似的内容将捕获的对象保存为 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];
【讨论】:
使用此代码,
NSData* pngdata = UIImagePNGRepresentation (image); //PNG wrap
UIImage* img = [UIImage imageWithData:pngdata];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
【讨论】: