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