1, judging whether the system supports the camera [Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera] 2. Create album view Controller object Uiimagepickercontroller *imagepickercontroller = [[Uiimagepickercontroller alloc] init];3, Set SourceType (resource type) Imagepickercontroller.sourcetype = uiimagepickercontrollersourcetypecamera;// Camera Imagepickercontroller.sourcetype = uiimagepickercontrollersourcetypephotolibrary;// Photo Gallery Imagepickercontroller.sourcetype = uiimagepickercontrollersourcetypesavedphotosalbum;//saved picture 4, Set pop-up animations (optional) Imagepickercontroller.modaltransitionstyle = Uimodaltransitionstylecoververtical;5, Set whether to allow editing of picture imagepickercontroller.allowsediting = yes;6, set proxy (uiimagepickercontrollerdelegate) Imagepickercontroller.delegate = self;7, Implementing proxy method: Get picture-(void) Imagepickercontroller: (Uiimagepickercontroller *) picker Didfinishpickingmediawithinfo: (nsdictionary *) info{ [Picker dismissViewControllerAnimated :yes completion:nil]; //Get pictures UIImage *image =[Info objectforkey:uiimagepickercontrolleroriginalimage];} Cancel click-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{ [self dismissviewcontrolleranimated:yes completion:^{}];} 8. Save picture to album Uiimagewritetosavedphotosalbum (image, Self, @selector (image:didFinishSavingWithError:contextInfo:), nil) ;//image: The picture Uimage object to save,-(void) Image: (UIImage *) Imagedidfinishsavingwitherror: (Nserror *) error ContextInfo: (void *) contextinfo{//if error==null, indicates that the picture was saved successfully} specific code:-(void) Viewdidload {
[Super Viewdidload];
UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
Btn.frame = CGRectMake (10, 50, 80, 80);
Btn.backgroundcolor = [Uicolor Redcolor]; [Btn addtarget:self Action: @selector (Showpicker) forcontrolevents:uicontroleventtouchupinside]; [Self.view addsubview:btn];}
-(void) showpicker{
Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc]init];
Imagepicker.sourcetype = 2;//Setting type (0 for Call album, 1 for pop-up camera)
Imagepicker.allowsediting = yes;//Whether to allow editing, default No, set to YES, click on the picture will go to the editing interface (cropping)
Imagepicker.delegate = self;//set agent [self presentviewcontroller:imagepicker animated:yes completion:nil];//pop-up album/Camera
}
Proxy method called after selecting a picture
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary< NSString *,id> *) info{
NSLog (@ "%@", info);
UIImage *image = info[@ "Uiimagepickercontrollereditedimage"];//get Pictures
Uiimageview *imageview = [[Uiimageview alloc]initwithimage:image];
Imageview.frame = CGRectMake (40, 150, 100, 100); [Self.view Addsubview:imageview]; [Self Dismissviewcontrolleranimated:yes completion:nil];//disappears Imagepicker}
How to call a local photo album, Camera