2013-03-12 43 views
1

如果我们使用iPhone相机拍摄图片,图像将默认保存为JPEG格式。使用iPhone相机拍摄的图像的设置类型

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

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

回答

4

这将帮助你保存图像为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]; 
} 
+0

我们可以使用PNG或JPEG格式保存为PNG或JPEG格式..但是我想知道的是,第一次保存应该是PNG格式..我对转换不感兴趣.. – Coder 2013-03-12 10:36:46

+1

@jithen,在上面的代码中没有图像格式的转换,它直接保存图像在PNG格式 – NAZIK 2013-03-12 10:39:03

+0

当用户在iPhone摄像头捕获图像,'didFinishPickingImage'将被调用,图像将直接保存在PNG格式,请参阅代码 – NAZIK 2013-03-12 10:43:05

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]; 
+0

如果我们有一个图像,那么我们可以使用上面的代码转换为PNG或JPEG格式...但我的疑问是如何从照相机键直接捕获PNG格式的图像...我们可以在捕获后进行转换。 。但我想直接在PNG格式图像没有任何转换... – Coder 2013-03-12 10:33:00

+0

@jithen检查了这一点? http://snipplr.com/view/60998/ – lifetimes 2013-03-12 10:34:43

1

使用此代码,

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

这是什么格式类型的“图像”在这里....它肯定会是JPEG,如果你从相机捕获的..我想知道的是,我们可以捕获使用相机以PNG格式显示图像?如果是的话,该怎么做 – Coder 2013-03-12 10:34:38

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,你可以将这些数据保存到特定的文件。欲了解更多详情,请通过此link

希望这会帮助你。

+0

哪里是jpeg数据??? – Rajneesh071 2013-03-12 10:29:28

+1

检查更新的答案。对于JPEG数据,压缩比率要求。有关更多详细信息,请参阅ans中附带的Apple参考链接。 – Mrunal 2013-03-12 10:33:04

+0

UIImagePickerControllerOriginalImage给出已保存的图像..我需要从PNG格式类型的摄像头捕获图像 – Coder 2013-03-12 10:35:41

相关问题