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