set type of an image captured using iPhone camera

北城以北 提交于 2019-12-10 10:53:53

问题


If we capture a picture using iPhone camera, image will be saved in JPEG format by default.

I would like to save the image captured in other formats like PNG. Is it possible?

Is it possible to do this from code when we invoke iPhone camera from our application, can we set image type it has to be saved after capturing picture? I mean to open Camera and set the type before capturing picture?


回答1:


This will help you to save image in png type

-(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];
}



回答2:


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

Once you get NSData, you can save this data to specific file. For more details go through this link

Hope this will help you.




回答3:


You could use something similar to this in your method to save the captured object as a 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];



回答4:


Use this code,

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


来源:https://stackoverflow.com/questions/15358527/set-type-of-an-image-captured-using-iphone-camera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!