IOS Image Upload and Processing

Source: Internet
Author: User

IOS Image Upload and Processing

 

To read images from a camera or from an album, you must use the UIImagePickerController class. When using UIImagePickerController, you must implement the following two Protocols:

 

 

 

 

 

1. Read images from the album

First, you need to instantiate the UIImagePickerController object imagePicker. Set the image source of imagepickercontrollersourcetypephotolibrary to UIImagePickerControllerSourceTypePhotoLibrary, indicating that the source of the current image is the user's album. And set whether the image can be edited by allowsEditing.

# Pragma mark-obtain an image from a user's album-(void) pickImageFromAlbum {imagePicker = [[UIImagePickerController alloc] init]; imagePicker. delegate = self; imagePicker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker. modalTransitionStyle = UIModalTransitionStyleCoverVertical; imagePicker. allowsEditing = YES; [self presentModalViewController: imagePicker animated: YES];}

2. Read images from the album

 

 

# Pragma mark-obtain an image from the camera-(void) pickImageFromCamera {imagePicker = [[UIImagePickerController alloc] init]; imagePicker. delegate = self; imagePicker. sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker. modalTransitionStyle = UIModalTransitionStyleCoverVertical; imagePicker. allowsEditing = YES; [self presentModalViewController: imagePicker animated: YES];} // enable camera-(IBAction) touch_photo :( id) sender {// for iphone UIImagePickerController * pickerImage = [[UIImagePickerController alloc] init]; if ([UIImagePickerController isSourceTypeAvailable: Custom]) {pickerImage. sourceType = UIImagePickerControllerSourceTypeCamera; pickerImage. mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: pickerImage. sourceType];} pickerImage. delegate = self; pickerImage. allowsEditing = YES; // custom photo style [self presentViewController: pickerImage animated: YES completion: nil];}

After the user has finished the image, the method for selecting the end will be called back.

 

 

-(Void) imagePickerController :( UIImagePickerController *) picker preview :( NSDictionary *) info {// initialize imageNew as the -- UIImage * imageNew = [info objectForKey: @ brief] obtained from the camera; // set the image size CGSize imagesize = imageNew. size; imageSize. height = 626; imageSize. width = 413; // compress the image size -- imageNew = [self imageWithImage: imageNew scaledToSize: imageSize]; NSData * imageData = UIImageJPEGRepresentation (imageNew, 0.00001 ); if (m_selectImage = nil) {m_selectImage = [UIImage imageWithData: imageData]; NSLog (@ m_selectImage: % @, m_selectImage); [self. takePhotoButton setImage: m_selectImage forState: UIControlStateNormal]; [picker dismissModalViewControllerAnimated: YES]; return ;}}

Compress images

 

 

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize{    // Create a graphics image context    UIGraphicsBeginImageContext(newSize);        // Tell the old image to draw in this new context, with the desired    // new size    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];        // Get the new image from the context    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();        // End the context    UIGraphicsEndImageContext();        // Return the new image.   return newImage;}

Save the image to the Documents directory and convert the PNG and JPEG formats.

 

 

-(Void) imagePickerController :( UIImagePickerController *) picker pointer :( NSDictionary *) info {NSString * mediaType = [info objectForKey: Encrypted]; if ([mediaType isEqualToString: @ public. image]) {image = [info objectForKey: @ UIImagePickerControllerOriginalImage]; NSData * data; if (UIImagePNGRepresentation (image) = nil) {data = UIImageJPEGRepresentation (image, 1 );} else {data = UIImagePNGRepresentation (image);} NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * filePath = [NSString stringWithString: [self getPath: @ image1]; // store the image to a local file. [fileManager createDirectoryAtPath: filePath attributes: YES attributes: nil error: nil]; [fileManager createFileAtPath: [filePath stringByAppendingString: @/image.png] contents: dataAttributes: nil]; UIImage * editedImage = [[UIImage alloc] init]; editedImage = image; CGRect rect = CGRectMake (0, 0, 64, 96); UIGraphicsBeginImageContext (rect. size); [editedImage drawInRect: rect]; editedImage = Response (); UIButton * imageButton = [UIButton buttonWithType: UIButtonTypeCustom]; imageButton. frame = CGRectMake (10, 10, 64, 96); [imageButton setImage: editedImage forState: UIControlStateNormal]; [self. view addSubview: imageButton]; [imageButton addTarget: self action: @ selector (imageAction :) forControlEvents: UIControlEventTouchUpInside]; [self effect: YES];} else {NSLog (@ Media );}

The image name and format cannot be obtained in the above method, so you need to convert it to NSData binary Storage

 

 

 image = [info objectForKey:@UIImagePickerControllerOriginalImage];NSData *data;        if (UIImagePNGRepresentation(image) == nil) {            data = UIImageJPEGRepresentation(image, 1);        } else {            data = UIImagePNGRepresentation(image);        }
[FileManager createFileAtPath: [filePath stringByAppendingString: @/image.png] contents: data attributes: nil]; // Save the image as PNG [fileManager createFileAtPath: [filePath stringByAppendingString: @/image.jpg] contents: data attributes: nil]; // Save the image as JPEG

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.