简体   繁体   中英

set type of an image captured using iPhone camera

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?

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.

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

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

Use this code,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM