iOS get photo album/Camera picture-------custom Get Picture Gizmo

Source: Internet
Author: User

First, Function introduction

1, the package of a button, click the button, you will be prompted to get the picture: If the device supports the camera, can be obtained from the camera, but also from the phone album to get pictures.

2, after the selection of pictures, there is a block callback, according to the needs of the obtained pictures to use.

3, provides the initialization method, can flexibly define the button, including the return picture set to the button oneself.

Second, the core principle

1. Uialertcontroller Prompt Box

2. Uiimagepickercontroller Picture Pickup Controller

3, Issourcetypeavailable:uiimagepickercontrollersourcetypecamera class method, determine whether to support the camera

4, abide by uiimagepickercontrollerdelegate,uinavigationcontrollerdelegate

5, implement the Agent method to obtain the picture:-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: ( Nsdictionary *) Info

6, UIImageJPEGRepresentation method will compress the image of the conversion layer NSData, and then write to the sandbox

Third, the source code

1. h file

@interfaceZqimagetoolsbutton:uibutton/** * You can get a dictionary of picture information, which contains the full path of the picture in the sandbox (the path of the program running the sandbox may be different, but the picture name will not change) * * @return*/-(Nsdictionary *) Getimageinfro;/** * Initialization method, click the button can choose from the album or camera (if there is a camera) select the picture and return the picture * * @param title button name * @param frame button Frame * @param contro Ller the controller that the button is on, the controller that pops up the dialog box * @param block to get the callback after the picture * @param imageName The name of the picture stored in the sandbox * * @return return a button*/-(Instancetype) Initwithtitle: (NSString *) title frame: (CGRect) Frame controller: (Uiviewcontroller *) controller Imageblock: (void(^) (UIImage *)) Block ImageName:(NSString *) ImageName;@end

2. m file

@interfaceZqimagetoolsbutton () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>@property (nonatomic,weak) Uiviewcontroller*Controller; @property (nonatomic,copy)void(^imageblock) (UIImage *); @property (Nonatomic,strong) NSString*ImageName, @property (nonatomic,strong) nsdictionary*Imageinfro;@end@implementationZqimagetoolsbutton-(Nsdictionary *) getimageinfro{returnSelf.imageinfro;}-(Instancetype) Initwithtitle: (NSString *) title frame: (CGRect) Frame controller: (Uiviewcontroller *) controller Imageblock: (void(^) (UIImage *)) Block ImageName:(NSString *) imagename{ Self=[[Zqimagetoolsbutton alloc]initwithframe:frame]; Self.imageblock=Block; Self.controller=Controller; Self.imagename=ImageName;        [Self Settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];        [Self settitle:title forstate:uicontrolstatenormal];        [Self addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside]; returnSelf ;}- (void) Btnclick: (UIButton *) btn{//Create a picture manager, jump to a camera or album pageUiimagepickercontroller *imagepickercontroller =[[Uiimagepickercontroller alloc] init]; Imagepickercontroller.Delegate=Self ; Imagepickercontroller.allowsediting=YES; //Create a bullet boxUialertcontroller* AV = [uialertcontroller alertcontrollerwithtitle:@"Select"Message@"Choose from your photo album/camera"Preferredstyle:uialertcontrollerstyleactionsheet]; //to set the source type of the image Manager__block Nsinteger sourcetype=0; Uialertaction* Photo = [Uialertaction actionwithtitle:@"My albums"Style:uialertactionstyledefault handler:^ (Uialertaction *_nonnull Action) {                        //Photo source for photo albumsourcetype=Uiimagepickercontrollersourcetypesavedphotosalbum; Imagepickercontroller.sourcetype=sourcetype; [Self.controller presentviewcontroller:imagepickercontroller Animated:yes Completion:^{}];            }]; Uialertaction* Camera = [Uialertaction actionwithtitle:@"my camera."Style:uialertactionstyledefault handler:^ (Uialertaction *_nonnull Action) {                        //picture Source for camerasourcetype=Uiimagepickercontrollersourcetypecamera; Imagepickercontroller.sourcetype=sourcetype; [Self.controller presentviewcontroller:imagepickercontroller Animated:yes Completion:^{}];        }]; Uialertaction* Cancle = [uialertaction actionwithtitle:@"Cancel"Style:uialertactionstylecancel handler:^ (Uialertaction *_nonnull Action) {        //Cancel    }]; //determine if the camera is supported        if([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {[AV addAction:ph        OTO];        [AV addaction:camera];            [AV addaction:cancle]; }    Else    {        //if the camera is not supported, you don't have to add the camera button to the Cue box.[av addaction:photo];    [AV addaction:cancle];    } [Self.controller Presentviewcontroller:av animated:yes Completion:nil]; }//after the picture selection is over, go this way, the dictionary holds all the picture information#pragmaMark-image Picker Delegte System Agent, returns the picture selected from the album-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info{[picker dismissviewcontrolleranimated:yes Completion:^{NSLog (@"I 've chosen the picture.");        }]; //get the selected picture! UIImage *image =[Info objectforkey:uiimagepickercontrolleroriginalimage]; //Block callback, after obtaining the picture, give the controllerSelf.imageblock (image); //Save the image locally, re-stitch the name of the image with the name of the pass inNSString* Imgname = [NSString stringWithFormat:@"Savedimage-%@.png", Self.imagename]; //stitching the path saved to the sandboxNSString *fullpath = [[Nshomedirectory () stringByAppendingPathComponent:@"Documents"] Stringbyappendingpathcomponent:imgname]; //record a piece of information, save it in a dictionary, you can access it when neededNsdictionary *dic = @{@"Image": FullPath}; Self.imageinfro=dic; //Save to Sandbox[self saveimage:image withname:imgname filepath:fullpath]; }//Save to Sandbox- (void) SaveImage: (UIImage *) currentimage withname: (NSString *) imageName FilePath: (NSString *) filepath{//Compression ratio 0.5NSData * ImageData = uiimagejpegrepresentation (Currentimage,0.5); //write a picture to a file[ImageData Writetofile:filepath atomically:no];}@end

Iv. expansion

1, this example does not realize the multiple selection of pictures

2, this example does not implement the business logic of uploading pictures

3, Small things for entertainment exchange use

iOS get photo album/Camera picture-------custom Get Picture Gizmo

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.