Use Uiimagepickercontroller for photo and video recording

Source: Internet
Author: User

A Uiimagepickercontroller Introduction and attribute introduction.

Uiimagepickercontroller inherits from Uinavigationcontroller. Uiimagepickercontroller can be used to select photos, which can also be used to take pictures and record videos. First look at the properties and methods commonly used in this class:

Property Description
@property (nonatomic) Uiimagepickercontrollersourcetype sourcetype Pick source Type, SourceType is enum type:
Uiimagepickercontrollersourcetypephotolibrary: Photo Gallery
, the default value
Uiimagepickercontrollersourcetypecamera: Webcam
Uiimagepickercontrollersourcetypesavedphotosalbum: Photo Albums
@property (nonatomic,copy)       nsarray                                *mediatypes media type, by default this array contains Kuttypeimage, so you can take photos without setting, but when you want to record the time must be set, can be set to Kuttypevideo (video, Without sound) or Kuttypemovie (video with sound)
@property (nonatomic)            nstimeinterval                          videomaximumduration video maximum recording duration, default is ten s
@property (nonatomic)            uiimagepickercontrollerqualitytype    videoquality Video quality, enumeration type:
Uiimagepickercontrollerqualitytypehigh: HD quality
Uiimagepickercontrollerqualitytypemedium: Medium quality, suitable for WiFi transmission
Uiimagepickercontrollerqualitytypelow: Low quality, suitable for cellular network transmission
uiimagepickercontrollerqualitytype640x480:640*480
uiimagepickercontrollerqualitytypeiframe1280x720:1280 *720
uiimagepickercontrollerqualitytypeiframe960x540:960*540
@property (nonatomic)            bool                                    Showscameracontrols If the camera control Panel is displayed, the default is Yes
@property (nonatomic,retain)     uiview                                  *cameraoverlayview  The view that is covered on the camera can be used to customize the photo or video interface via this video
@property (nonatomic) Cgaffinetransform Cameraviewtransform Camera deformation
@property (nonatomic) Uiimagepickercontrollercameracapturemode Cameracapturemode Camera capture mode, capture mode is enum type:
Uiimagepickercontrollercameracapturemodephoto: Photo Mode
Uiimagepickercontrollercameracapturemodevideo: Video recording mode
@property (nonatomic) uiimagepickercontrollercameradevice      Cameradevice camera device, Cameradevice is an enumeration type:
Uiimagepickercontrollercameradevicerear: Front-facing camera
Uiimagepickercontrollercameradevicefront: Rear-facing camera
@property (nonatomic) Uiimagepickercontrollercameraflashmode Cameraflashmode Flash mode, Enumeration type:
Uiimagepickercontrollercameraflashmodeoff: Turn off the flash
Uiimagepickercontrollercameraflashmodeauto: Flash Automatic
Uiimagepickercontrollercameraflashmodeon: Turn on the flash
Class method Description
+ (BOOL) issourcetypeavailable: (Uiimagepickercontrollersourcetype) sourcetype Whether the specified source type is available, SourceType is an enumeration type:
Uiimagepickercontrollersourcetypephotolibrary: Photo Gallery
Uiimagepickercontrollersourcetypecamera: Webcam
Uiimagepickercontrollersourcetypesavedphotosalbum: Photo Albums
+ (Nsarray *) Availablemediatypesforsourcetype: (Uiimagepickercontrollersourcetype) sourcetype The media type that is available on the specified source device, typically pictures and videos
+ (BOOL) iscameradeviceavailable: (Uiimagepickercontrollercameradevice) Cameradevice Specifies whether the camera is available, Cameradevice is an enumeration type:
Uiimagepickercontrollercameradevicerear: Front-facing camera
Uiimagepickercontrollercameradevicefront: Rear-facing camera
+ (BOOL) Isflashavailableforcameradevice: (Uiimagepickercontrollercameradevice) Cameradevice Specifies whether the camera's flash is available
+ (Nsarray *) Availablecapturemodesforcameradevice: (Uiimagepickercontrollercameradevice) CameraDevice Gets the available capture mode on the specified camera, which is the enumeration type:
Uiimagepickercontrollercameracapturemodephoto: Photo Mode
Uiimagepickercontrollercameracapturemodevideo: Video recording mode
Object methods Description
-(void) takepicture Take pictures programmatically
-(BOOL) startvideocapture Recording video Programmatically
-(void) stopvideocapture Stop recording Video programmatically
Proxy method Description
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info Media pickup Complete
-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker Cancel Pickup
Extension methods (primarily for saving photos, videos to albums) Description
Uiimagewritetosavedphotosalbum (UIImage *image, id completiontarget, SEL completionselector, void *contextinfo) Save photos to albums
Uivideoatpathiscompatiblewithsavedphotosalbum (NSString *videopath) Can I save a video to a photo album
void Uisavevideoatpathtosavedphotosalbum (NSString *videopath, id completiontarget, SEL completionselector, void * ContextInfo) Save Video to Photo album

Two use Uiimagepickercontroller to take pictures and video recording

#import "ViewController.h" #import <MobileCoreServices/MobileCoreServices.h> #import <avfoundation/ Avfoundation.h> @interface Viewcontroller () <uiimagepickercontrollerdelegate,uinavigationcontrollerdelegate > @property (Weak, nonatomic) Iboutlet Uiimageview *mimageview; @property (nonatomic,assign) BOOL istakevedio;@ Property (Nonatomic,strong) Uiimagepickercontroller *imagepicker; @property (nonatomic,strong) Avplayer *player;@    End@implementation viewcontroller-(void) viewdidload {[Super viewdidload]; Do any additional setup after loading the view, typically from a nib.}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.}    -(Ibaction) Takepic: (ID) Sender {_istakevedio = NO; [Self showpicker];}    -(Ibaction) Takevedio: (ID) Sender {_istakevedio = YES; [Self showpicker];} -(Uiimagepickercontroller *) imagepicker{if (_imagepicker = = nil) {_imagepicker = [[Uiimagepickercontroller al]LOC] init];    _imagepicker.sourcetype = Uiimagepickercontrollersourcetypecamera; Set the source to camera _imagepicker.cameradevice = uiimagepickercontrollercameradevicerear;    Set the camera used as: Rear camera if (self.istakevedio) {_imagepicker.mediatypes = @[(NSString *) Kuttypevideo]; Set to video mode-<span style= "Color:rgb (51, 51, 51); Font-family:georgia, ' Times New Roman ', Times, Sans-serif; font-size:14px; line-height:25px; " > Note Media type definition in mobilecoreservices.framework </span> _imagepicker.videoquality = uiimagepickercontrollerqual   itytypeiframe1280x720;  Set Video Quality _imagepicker.cameracapturemode = Uiimagepickercontrollercameracapturemodevideo; Set camera mode to record video} else{_imagepicker.cameracapturemode = UIIMAGEPICKERCONTROLLERCAMERACAPTUREMODEP Hoto Set camera mode to take picture}} return _imagepicker;} -(void) showpicker{[self presentViewController:self.imagePicker animated:yes completion:nil];} #pragma mark-#pragma mark-uiimagepickercontrollerdelegate-(void) Imagepickercontroller: (Uiimagepickercontroller *) picker Didfinishpickingmediawithinfo: (nsdictionary *) info{nsstring *type = [Info objectforkey:    Uiimagepickercontrollermediatype];        if ([Type isequaltostring: (NSString *) Kuttypeimage]) {//Picture is saved and displayed UIImage *image;        if (picker.allowsediting) {image = [info objectforkey:uiimagepickercontrollereditedimage];//Allow editing, get edited picture        } else{image = [info objectforkey:uiimagepickercontrolleroriginalimage];//Edit not allowed, get original picture}        Self.mImageview.image = image;    Uiimagewritetosavedphotosalbum (Image,nil,nil, nil); } else if ([Type isequaltostring: (NSString *) Kuttypevideo]) {//video is saved after the video is played nsurl *url = [Info objectforkey:        Uiimagepickercontrollermediaurl];        NSString *urlpath = [url path]; if (Uivideoatpathiscompatiblewithsavedphotosalbum (URLPath)) {Uisavevideoatpathtosavedphotosalbum (UrlPath, self , @selector (videO:didfinishsavingwitherror:contextinfo:), nil);  }}}-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{} #pragma mark-#pragma mark-share method//video after saving callback-(void) Video: (NSString *) Videopath didfinishsavingwitherror: (nserror *) error ContextInfo: (void *)    contextinfo{if (error) {NSLog (@ "Save video errors, error message:%@", error.localizeddescription);        }else{NSLog (@ "video saved successfully.");        Automatically play Nsurl *url=[nsurl Fileurlwithpath:videopath] when finished recording;        _player=[avplayer Playerwithurl:url];        Avplayerlayer *playerlayer=[avplayerlayer Playerlayerwithplayer:_player];        Playerlayer.frame = Self.mImageview.frame;        [Self.mImageview.layer Addsublayer:playerlayer];    [_player play]; }}


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

Use Uiimagepickercontroller for photo and video recording

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.