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

Source: Internet
Author: User
Tags pngimage

Let's look at the following code:

-(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 "]; // Store a picture locally Documents

[FileManager createdirectoryatpath: FilePath withintermediatedirectories:yes< /c12> attributes:nil error:nil];

[FileManager createfileatpath: [FilePath stringbyappendingstring:@ "/image.png" ] contents:d ataattributes:nil];

UIImage *editedimage = [[UIImage alloc] init];

Editedimage = image;

cgrect rect = cgrectmake(0, 0, .) ;

uigraphicsbeginimagecontext(rect.size);

[Editedimage drawinrect: rect];

editedimage = Uigraphicsgetimagefromcurrentimagecontext();

UIButton *imagebutton = [UIButton buttonwithtype:Uibuttontyp Ecustom];

ImageButton. frame = CGRectMake(ten, ten, +) ;

[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 picture above we can not get the image name, and not clear image format, so this time 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 PNG format picture to binary, if the image format is JPEG then return nil;

[FileManager createfileatpath: [FilePath stringbyappendingstring:@ "/image.png"     ] contents:d ata attributes:nil]; Save a picture in PNG format

[FileManager createfileatpath: [FilePath stringbyappendingstring:@ "/   Image.jpg "] contents:d ata 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];

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.