set type of an image captured using iPhone camera
11,051
Solution 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];
}
Solution 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.
Solution 3
Use this code,
NSData* pngdata = UIImagePNGRepresentation (image); //PNG wrap
UIImage* img = [UIImage imageWithData:pngdata];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
Solution 4
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];
Related videos on Youtube
Author by
Coder
Updated on June 04, 2022Comments
-
Coder almost 2 years
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?
-
Mrunal about 11 yearsCheck updated ans. For JPEG data, compression ratio requires. For more details go through the apple reference link attached in ans.
-
Coder about 11 yearsWhats the format type of "image" here.... It will be JPEG for sure if u captured that from camera.. what i want to know is, can we capture image in PNG format using camera? If yes, how to do that
-
Coder about 11 yearsUIImagePickerControllerOriginalImage gives already saved image.. I need capturing image from camera with PNG format type
-
Coder about 11 yearsWe can save in PNG or JPEG using above PNG or JPEG Representations.. But all i want to know is, first time save should be in PNG format.. I am not interested in conversions..
-
Mrunal about 11 yearsUIImagePickerControllerOriginalImage - It will provide you image in object format UIImage, which is not yet stored anywhere. To save that image you will require to writeToFile. Before doing that you can convert your UIImage to NSData using given two methods. Those methods convert your UIImage object to specific image type (PNG/Jpeg) data.
-
Nazik about 11 years@jithen, in the above code there is no conversion of image format, it directly saves the image in png format
-
Mrunal about 11 yearsOnce you got the NSData format, save your file using writeToFile method. Provide fileName+extension as per your selected conversion (PNG/Jpeg)
-
Nazik about 11 yearswhen the user captures the image in iphone camera, the
didFinishPickingImage
will be called, the image will be directly saved in png format, see the code