IOS Image Upload processing Image Compression Image Processing

Source: Internet
Author: User
Tags pngimage

It is mentioned that obtaining images from cameras/albums is intended for end users. Users can browse and select images as programs. Here, we need to use the UIImagePickerController class to interact with users.
To use UIImagePickerController to interact with users, we need to implement two protocols <UIImagePickerControllerDelegate, UINavigationControllerDelegate>.

 

View Code

 

The Code is as follows:

 

# Pragma mark obtains activity images from the 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];

}

 

Let's take a look at the above picture retrieval from the album. First, we need to instantiate the UIImagePickerController object, set the imagePicker object to the current object, and set the imagePicker image source to UIImagePickerControllerSourceTypePhotoLibrary, indicates that the source of the current image is an album. In addition, you can also set whether the image can be edited.

View Code

 

The Code is as follows:

# Pragma mark obtains activity images 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];

}

 

// Open the camera

-(IBAction) touch_photo :( id) sender {

// For iphone

UIImagePickerController * pickerImage = [[UIImagePickerController alloc] init];

If ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {

PickerImage. sourceType = UIImagePickerControllerSourceTypeCamera;

PickerImage. mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: pickerImage. sourceType];


}

PickerImage. delegate = self;

PickerImage. allowsEditing = YES; // customize the photo Style

[Self presentViewController: pickerImage animated: YES completion: nil];

}

 

The above figure shows how to get an image from the camera. It is different from how to get an image from the album. The source of the camera image is UIImagePickerControllerSourceTypeCamera.

After interacting with the user, after the user selects an image, the method of ending is called back.

 

-(Void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info

{

// Initialize imageNew as obtained from the camera --

UIImage * imageNew = [info objectForKey: @ "UIImagePickerControllerOriginalImage"];

// 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. TakePhotoBtn setImage: m_selectImage forState: UIControlStateNormal];

[Picker dismissModalViewControllerAnimated: YES];

Return;

}

[Picker release];

}

// Compress the image size --

-(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 local document-and convert the image format


For IOS developers, save images to the Documents directory and PNG. JPEG format conversion-(void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info {

NSString * mediaType = [info objectForKey: UIImagePickerControllerMediaType];

If ([mediaType isw.tostring: @ "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"]; // stores images to local documents

 


[FileManager createDirectoryAtPath: filePath withIntermediateDirectories: 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 = UIGraphicsGetImageFromCurrentImageContext ();

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

 


[Ipc dismissModalViewControllerAnimated: YES];

} Else {

NSLog (@ "MEdia ");

}

The code above is to save the image to the Local Sandbox after selecting the image from the album. The image name and the image format cannot be obtained from the image we obtained above, therefore, we 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 );

}

UIImagePNGRepresentation: convert an image in PNG format to binary. If the image format is JPEG, nil is returned;


[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

 

 

You can also save images in the following format:

NSString * pngImage = [filePath stringByAppendingPathComponent: @ "Documents/image.png"];

NSString * jpgImage = [filePath stringByAppendingPathComponent: @ "Documents/image.jpg"];

 

 

[Data writeToFile: pngImage atomically: YES];

[Data writeToFile: jpgImage atomically: YES];

 

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.