iOS image upload and compression

Source: Internet
Author: User

mentioned from the camera/album to get the picture is for the end user, by the user to browse and select the image for the program to use. Here we need the Uiimagepickercontroller class to interact with the user.

With Uiimagepickercontroller and user interaction, we need to implement 2 protocols <uiimagepickercontrollerdelegate,uinavigationcontrollerdelegate >.

?

View Code

Code to copy code as follows

#pragma mark gets the active picture from the user 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 to get the picture from the album, we first instantiate the Uiimagepickercontroller object, then set the Imagepicker object as the current object, Set Imagepicker's picture source as Uiimagepickercontrollersourcetypephotolibrary, indicating that the current picture is from a photo album, in addition to setting whether the user can edit the picture.

View Code

Code to copy code as follows

#pragma mark gets the active picture 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];

}

Turn on 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;//Custom Photo styles

[Self presentviewcontroller:pickerimage animated:YEScompletion:nil];

}

The above is from the camera to get pictures, and from the album to get pictures just picture source settings are not the same, the camera image from the source for Uiimagepickercontrollersourcetypecamera.

After interacting with the user, the user selects a good picture and then recalls the method of selecting end.

-(void) Imagepickercontroller: (uiimagepickercontroller*) Picker didfinishpickingmediawithinfo: (NSDictionary*) Info

{

Initialize the imagenew to be obtained from the camera--

UIImage *imagenew = [info objectforkey:@ "Uiimagepickercontrolleroriginalimage"];

Set the size of an image

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;

}

Image saved to local document--and image format conversion

Save images for iOS development to the documents directory and PNG,JPEG format to convert each other

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary*) Info {

NSString *mediatype = [info objectforkey:uiimagepickercontrollermediatype];

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"]]; Save a picture to a 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 above code is when you choose a picture from the album to save to the local program sandbox, in the above we get the picture can not get the name of the picture,
and unclear picture format, so this time we need to convert it into NSData binary storage,

image = [Info objectforkey:@ "Uiimagepickercontrolleroriginalimage"];

NSData *data;

if (uiimagepngrepresentation (image) = = nil) {

data = uiimagejpegrepresentation (image, 1);

} else {

data = uiimagepngrepresentation (image);

}

Uiimagepngrepresentation convert PNG format picture to binary, if the image format is JPEG then return nil;

[FileManager createfileatpath:[filepath stringbyappendingstring:@ "/image.png"] contents:data Attributes:nil]; Save a picture in PNG format

[FileManager createfileatpath:[filepath stringbyappendingstring:@ "/image.jpg"] contents:data Attributes:nil]; Save a picture in JPEG format

We can also write the following format to store pictures

NSString *pngimage = [FilePath stringbyappendingpathcomponent:@ "Documents/image.png"];

NSString *jpgimage = [FilePath stringbyappendingpathcomponent:@ "documents/image.jpg"];

[Data writetofile:pngimage Atomically:yes];

[Data writetofile:jpgimage Atomically:yes];

iOS image upload and compression

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.