Examples of iOS access to photo albums and photo-saving implementations

Source: Internet
Author: User

Examples of iOS access to photo albums and photo-saving implementations

Implementation results:
1, click the Access album button, you can access the System album, choose to return in the ImageView display
2, click to take photos, access to the camera, the implementation will be saved in the album, Back in the ImageView display

Note: The camera function requires real-computer debugging, the simulator cannot be implemented
The simulator will have the following effect ~ Popup warning Box

Project download: GitHub project Download link

Here is the program: note that the two buttons and Uiimageview in this example are added in storyboard;

ViewController.h

@interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>@property (weaknonatomicIBOutletUIImageView *imageShow;@end

Viewcontroller.m

#pragma mark-take photos and save- (ibaction) Takephotoaction: (ID) Sender {BOOLIscamera = [Uiimagepickercontroller iscameradeviceavailable:uiimagepickercontrollercameradevicerear];if(!iscamera) {//If not available, pop-up warning boxUialertview *alert = [[Uialertview alloc] initwithtitle:@"No camera available"MessageNilDelegate Selfcancelbuttontitle:@"OK"Otherbuttontitles:Nil,Nil]; [Alert show];return;    } Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc] init]; Imagepicker. SourceType= Uiimagepickercontrollersourcetypecamera;/** * uiimagepickercontrollersourcetypephotolibrary all Resource folders Uiimagepickercontrollersourcetyp Ecamera, Camera uiimagepickercontrollersourcetypesavedphotosalbum, built-in album * *Imagepicker. Delegate= Self;//Set up agent, follow Uinavigationcontrollerdelegate,uiimagepickercontrollerdelegate protocol[ SelfPresentviewcontroller:imagepicker Animated:YESCompletion:Nil];}
#pragma mark - 访问相册- (IBAction)browseAlbum:(id)sender {    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;    imagePicker.delegateself;    [self presentViewController:imagePicker animated:YES completion:nil];}
#pragma mark-Implementation of the Protocol method//Protocol method, after selection, presented in Imageshow- (void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (nsdictionary*) Info {NSLog(@"%@", info);//uiimagepickercontrollermediatype,uiimagepickercontrolleroriginalimage, Uiimagepickercontrollerreferenceurl    NSString*mediatype = info[@"Uiimagepickercontrollermediatype"];if([MediaType isequaltostring:@"Public.image"]) {//Determine if it is a picture        UIImage*image = [info objectforkey:uiimagepickercontrolleroriginalimage]; Self. Imageshow. Image= Image;//By judging the sourcetype of picker, if it is photographed then save to the album to        if(Picker. SourceType= = Uiimagepickercontrollersourcetypecamera) {uiimagewritetosavedphotosalbum (image, Self,@selector(Image:didFinishSavingWithError:contextInfo:),Nil); }    }//Else of course it may be video, not discussed here ~ method is similar ~[Picker dismissviewcontrolleranimated:YESCompletion:Nil];}//This method is above the Uiimagewritetosavedphotosalbum- (void) Image: (UIImage*) Image didfinishsavingwitherror: (Nserror*) Error ContextInfo: (void*) ContextInfo {NSLog(@"Saved");}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Examples of iOS access to photo albums and photo-saving implementations

Related Article

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.