IOS Save pictures to albums

Source: Internet
Author: User

 IOS Save pictures to albums 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:
1 voidUIImageWriteToSavedPhotosAlbum (
2    UIImage  *image,
3    id       completionTarget,
4    SEL      completionSelector,
5    void*contextInfo
6 );
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
1 - (void) image: (UIImage *) image
2 didFinishSavingWithError: (NSError *) error
3              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:

01 - (void)saveImageToPhotos:(UIImage*)savedImage
02 {
03     UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); 
04 }
05 // 指定回调方法
06 - (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void*) contextInfo
07 {  
08     NSString *msg = nil ;  
09     if(error != NULL){
10         msg = @"保存图片失败";
11     }else{
12         msg = @"保存图片成功";
13     }
14     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
15                                                         message:msg
"                                                            delegate:self
+                                                   cancelbuttontitle:@ "OK"
18                                               otherButtonTitles:nil];
19     [alert show];
20 }
21
22 // 调用示例
23 UIImage *savedImage = [UIImage imageNamed:"savedImage.png"];
24
25 [self saveImageToPhotos:savedImage];

IOS Save pictures to albums

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.