Save images with Uiimagepickercontroller or with Uikit Uigraphicsbeginimagecontext

Source: Internet
Author: User

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

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.