Reprinted from: http://my.oschina.net/hmj/blog/99970
Sometimes we have the need to save images, such as using Uiimagepickercontroller to take pictures with a built-in camera on an iOS device, or sometimes we take advantage of Uikit's uigraphicsbeginimagecontext in the app, The Uigraphicsendimagecontext,uigraphicsgetimagefromcurrentimagecontext method creates an image that needs to be saved. The iOS Uikit framework provides the Uiimagewritetosavedphotosalbum method for saving images, which saves the image to the user's album, as described below:
void uiimagewritetosavedphotosalbum ( UIImage *image, ID Completiontarget, SEL completionselector, void *contextinfo);
Parameter description:
Image
Uimage object with saved picture
Completiontarget
After saving the image to the album, call Completiontarget specified selector (optional)
Completionselector
The Completiontarget method corresponds to the selector, equivalent to the callback method, the following format should be satisfied
-(void) Image: (UIImage **) Error ContextInfo: (void *) contextinfo;
CONTEXTINFO Specifies that incoming data can be selected in the callback.
When we need to asynchronously get the message of the image save result, we need to specify the Completiontarget object and its completionselector corresponding selector. Examples are as follows:
- (void) Saveimagetophotos: (uiimage*) savedimage{uiimagewritetosavedphotosalbum (image, Self, @selector (image:didFinishSavingWithError:contextInfo :), NULL);}//Specifying callback Methods- (voidImage: (UIImage *) image didfinishsavingwitherror: (nserror *) Error ContextInfo: (void*) contextinfo{NSString*msg =Nil; if(Error! =NULL) {msg=@"failed to save picture" ; }Else{msg=@"Save Picture Success" ; } Uialertview*alert = [[Uialertview alloc] Initwithtitle:@"Save picture result hint"message:msg Delegate: Self cancelbuttontitle:@"Determine"Otherbuttontitles:nil]; [Alert show];} //Invoke ExampleUIImage *savedimage = [UIImage imagenamed:"Savedimage.png"]; [Self saveimagetophotos:savedimage];
Save images with Uiimagepickercontroller or with Uikit Uigraphicsbeginimagecontext