IOS SDK detailed photo/album (default + custom photo screen)

Source: Internet
Author: User

Original blog, reproduced please indicate the source
Blog.csdn.net/hello_hwc

Objective:
Originally want to update Nsurlsession's uploadtask, the result of writing the demo when want to write a photo upload, and then thought to write a picture of the demo. This article describes how to use the system-provided interface to take photos and select albums, and then customize the camera interface. Note that this article uses Uiimagepickercontroller, so it is not completely customizable, if you want to completely customize the photo, it is recommended to choose the AV Foundation this framework to do

Demo effect
Access to the system's photo screen

Go to Custom photo screen

Custom front camera and rear camera toggle Animation-Flip Page

One using the system-provided interface for photo and album selection

The first step
Save an instance of Uiimagepickercontroller and initialize it when appropriate. The demo selection is initialized in Viewdidload. Let the current class implement uiimagepickercontrollerdelegate,uinavigationcontrollerdelegate two agents

@property (strong,nonatomic)UIImagePickerController * imagePikerViewController;//初始化self.imagePikerViewController = [[UIImagePickerController alloc] init];self.imagePikerViewController.delegateself;//通过代理来传递拍照的图片self.imagePikerViewController.allowsEditingYES;//允许编辑

The second step, through the Actionsheet to let the user choose to be photographed or to the album selection, and then the modal display

[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];

Note that you need to determine if the camera is available and then enter the camera (it is possible that the camera is broken or running on the virtual machine)

Uialertcontroller * Alertcontroller = [Uialertcontroller alertcontrollerwithtitle:NilMessageNilPreferredstyle:uialertcontrollerstyleactionsheet]; [Alertcontroller addaction: [uialertaction actionwithtitle: @"Take Photo"Style:uialertactionstyledefault handler:^ (uialertaction *action) {if([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) { Self. Imagepikerviewcontroller. SourceType= Uiimagepickercontrollersourcetypecamera; [ SelfPresentviewcontroller: Self. ImagepikerviewcontrollerAnimated:YESCompletion:NULL]; }Else{            [ Selfshowalertwithmessage:@"Camera is not available in this device or simulator"];    }    }]]; [Alertcontroller addaction: [uialertaction actionwithtitle: @"Choose Existing Photo"Style:uialertactionstyledefault handler:^ (uialertaction *action) {if([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary]) { Self. Imagepikerviewcontroller. SourceType= Uiimagepickercontrollersourcetypephotolibrary; [ SelfPresentviewcontroller: Self. ImagepikerviewcontrollerAnimated:YESCompletion:NULL];    }    }]]; [Alertcontroller addaction: [uialertaction actionwithtitle: @"Cancel"Style:uialertactionstylecancel handler:Nil]]; [ SelfPresentviewcontroller:alertcontroller Animated:YESCompletion:Nil];

Part III, the agent function handles a photo or cancel event

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (nsdictionary*) info{UIImage* image = Info[uiimagepickercontrollereditedimage];if(!image)    {image = Info[uiimagepickercontrolleroriginalimage]; } Self. ImageView. Image= Image; [ SelfDismissviewcontrolleranimated:YESCompletion:NULL];} -(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{[ SelfDismissviewcontrolleranimated:YESCompletion:NULL];}
Two Custom photo screen

Uiimagepickercontroller's custom interface is simple, and can be customized by setting the Cameraoverlayview property for a custom view.
The first step is to create a view
Create a Xib file

Drag and drop controls for AutoLayout, final effect

Set Fileowner to Customtakephotoviewcontroller

The second step is to set the UI to what you want before displaying the photo screen, and note that the properties
The showscameracontrols is set to No, so the default interface does not appear.

 Self. Imagepikerviewcontroller. SourceType= Uiimagepickercontrollersourcetypecamera; Self. Imagepikerviewcontroller. Showscameracontrols=NO; [[NSBundleMainbundle] loadnibnamed:@"Customoverlayview"Owner SelfOptionsNil]; Self. Takepicturebutton. Layer. Cornerradius= -; Self. Takepicturebutton. Clipstobounds=YES; Self. Overlayview. Frame= Self. Imagepikerviewcontroller. Cameraoverlayview. Frame; Self. Overlayview. BackgroundColor= [UicolorClearcolor]; Self. Imagepikerviewcontroller. Cameraoverlayview= Self. Overlayview; Self. Overlayview=Nil; [ SelfPresentviewcontroller: Self. ImagepikerviewcontrollerAnimated:YESCompletion:NULL];

Step three, add actions to the controls on the view
Segment control is responsible for switching the front camera and rear camera, in order to smooth, in the switch to display the animation.

- (ibaction) Cameraselect: (Uisegmentedcontrol*) sender{Nsintegerindex = Sender. Selectedsegmentindex;if(Index = =0) {        [UIViewTransitionwithview: Self. Imagepikerviewcontroller. ViewDuration1.0options:uiviewanimationoptionallowanimatedcontent | Uiviewanimationoptiontransitioncurldown animations:^{ Self. Imagepikerviewcontroller. Cameradevice= Uiimagepickercontrollercameradevicefront; } Completion:NULL]; }Else{        [UIViewTransitionwithview: Self. Imagepikerviewcontroller. ViewDuration1.0options:uiviewanimationoptionallowanimatedcontent | Uiviewanimationoptiontransitioncurlup animations:^{[ Self. ImagepikerviewcontrollerSetcameradevice:uiimagepickercontrollercameradevicerear]; } Completion:NULL]; }}

button for taking pictures

- (IBAction)takePicture:(id)sender {    [self.imagePikerViewController takePicture];}

The button that was canceled

- (IBAction)cancelTakePicture:(id)sender {    [self dismissViewControllerAnimated:YES completion:NULL];}

Fourth step processing pictures in the agent

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{    UIImage * image = info[UIImagePickerControllerEditedImage];    if (!image) {        image = info[UIImagePickerControllerOriginalImage];    }    self.imageview.image = image;    [self dismissViewControllerAnimated:YES completion:NULL];}

Note that if the log output

anotinanemptyatbeforeorafter screen updates.

Just ignore it, no impact, seemingly a bug in iOS 8

Download link
http://download.csdn.net/detail/hello_hwc/8553539

IOS SDK detailed photo/album (default + custom photo screen)

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.