An analysis of iOS camera and album-uiimagepickercontroller-

Source: Internet
Author: User

Original address: http://blog.sina.com.cn/s/blog_7b9d64af0101cfd9.html-Analysis Zhen Wanderers


In some applications, we need to use the camera of the iOS device to take pictures, videos. and choose the pictures or videos we need from the album.
For iOS cameras and album apps, you can use the Uiimagepickercontroller class to complete control.
Related knowledge about Uiimagepickercontroller,
As follows:


Some iOS devices have cameras installed on them. Most of them are now.
In programming, we use the corresponding things to take photos, video and other functions.


First, Uiimagepickercontroller class
Uiimagepickercontroller This class can provide you with the function of photography, as well as pictures, video browsing functions.


Second, check whether the hardware is installed with a camera or allow to operate the album


These public methods, we may use, I put on! So easy!!!


#pragma mark-public classes related to cameras and albums


Determine if the device has a camera
-(BOOL) iscameraavailable{
return [Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera];
}


Whether the front camera is available
-(BOOL) isfrontcameraavailable{
return [Uiimagepickercontrolleriscameradeviceavailable:uiimagepickercontrollercameradevicefront];
}


Whether the rear camera is available
-(BOOL) isrearcameraavailable{
return [Uiimagepickercontrolleriscameradeviceavailable:uiimagepickercontrollercameradevicerear];
}




Determine if a type of multimedia is supported: Photo, video
-(BOOL) Camerasupportsmedia: (NSString *) Parammediatype sourcetype: (Uiimagepickercontrollersourcetype) paramsourcetype{
__block BOOL result = NO;
if ([parammediatype length] = = 0) {
NSLog (@ "Media type is empty.");
return NO;
}
Nsarray *availablemediatypes =[uiimagepickercontrolleravailablemediatypesforsourcetype:paramsourcetype];
[Availablemediatypes enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, bool*stop) {
NSString *mediatype = (NSString *) obj;
if ([Mediatypeisequaltostring:parammediatype]) {
result = YES;
*stop= YES;
}

}];
return result;
}


Check if the camera supports recording
-(BOOL) doescamerasupportshootingvideos{
return [self Camerasupportsmedia: ( NSString *) Kuttypemoviesourcetype:uiimagepickercontrollersourcetypecamera];
}


Check if camera supports taking pictures
-(BOOL) doescamerasupporttakingphotos{
return [self Camerasupportsmedia: ( NSString *) Kuttypeimagesourcetype:uiimagepickercontrollersourcetypecamera];
}


#pragma mark-album File selection related
Whether the album is available
-(BOOL) isphotolibraryavailable{
return [Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary];
}


Whether you can select a video in the album
-(BOOL) canuserpickvideosfromphotolibrary{
return [self Camerasupportsmedia: ( NSString *) Kuttypemovie sourcetype:uiimagepickercontrollersourcetypephotolibrary];
}


Whether you can select a video in the album
-(BOOL) canuserpickphotosfromphotolibrary{
return [self Camerasupportsmedia: ( NSString *) Kuttypeimage sourcetype:uiimagepickercontrollersourcetypephotolibrary];
}


Third, the camera for photo and video functions


1. We write the Uiimagepickercontroller function in the Click event of a button:



#pragma mark-Photo button event


-(void) Clickcontrolaction: (ID) sender{
The camera is judged and the photo function is supported
if ([self iscameraavailable] && [self doescamerasupporttakingphotos]) {
Initialize Picture selection Controller
Uiimagepickercontroller *controller = [[Uiimagepickercontroller alloc] init];
[Controller setsourcetype:uiimagepickercontrollersourcetypecamera];//Setup Type



Set the supported types, set only to take pictures, or only video, or both
NSString *requiredmediatype = (NSString *) kuttypeimage;
NSString *requiredmediatype1 = (NSString *) Kuttypemovie;
Nsarray *arrmediatypes=[nsarray Arraywithobjects:requiredmediatype, Requiredmediatype1,nil];
[Controller setmediatypes:arrmediatypes];

Set the quality of recorded video
[Controller Setvideoquality:uiimagepickercontrollerqualitytypehigh];
Setting the maximum camera time
[Controller SETVIDEOMAXIMUMDURATION:10.F];



[Controller setallowsediting:yes];//settings can manage images or videos that already exist
[Controller setdelegate:self];//Setup Agent
[Self.navigationcontroller Presentmodalviewcontroller:controller Animated:yes];
[Controller release];
} else {
NSLog (@ "Camera is not available.");
}
}




Explain:


2. Setsourcetype method


By setting the Setsourcetype method, you can determine the interface that is displayed by the called Uiimagepickercontroller.


typedef ns_enum (Nsinteger, Uiimagepickercontrollersourcetype) {
Uiimagepickercontrollersourcetypephotolibrary,
Uiimagepickercontrollersourcetypecamera,
Uiimagepickercontrollersourcetypesavedphotosalbum
};


respectively: Picture list, camera, camera album


3.setMediaTypes method


Set the supported types, set only to take pictures, or only video, or both
NSString *requiredmediatype = (NSString *) kuttypeimage;
NSString *requiredmediatype1 = (NSString *) Kuttypemovie;
Nsarray *arrmediatypes=[nsarray Arraywithobjects:requiredmediatype, Requiredmediatype1,nil];
[Controller setmediatypes:arrmediatypes];




4. About the Uiimagepickercontrollerdelegate agreement


We're going to store the photos and videos we've taken, so we're going to implement the Uiimagepickercontrollerdelegate protocol approach.


#pragma mark-uiimagepickercontrollerdelegate Proxy method




After you save the picture to the album, call the relevant method to see if the save was successful
-(void) imagewassavedsuccessfully: (UIImage *) paramimage didfinishsavingwitherror: (Nserror *) Paramerror ContextInfo :(void *) paramcontextinfo{
if (Paramerror = = nil) {
NSLog (@ "Image was saved successfully.");
} else {
NSLog (@ "An error happened while saving the image.");
NSLog (@ "Error =%@", paramerror);
}
}


When you get a photo or video, call the method
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info {
NSLog (@ "Picker returned successfully.");
NSLog (@ "%@", info);
NSString *mediatype = [info objectforkey:uiimagepickercontrollermediatype];
Judging get type: Picture
if ([MediaType isequaltostring: ( NSString *) Kuttypeimage]) {
UIImage *theimage = nil;
Determine if the image is allowed to be modified
if ([Picker allowsediting]) {
Get the image after the user edits
Theimage = [info objectforkey:uiimagepickercontrollereditedimage];
} else {
Metadata parameters for photos
Theimage = [info objectforkey:uiimagepickercontrolleroriginalimage];

}

Save the picture to the album
SEL Selectortocall = @selector (imageWasSavedSuccessfully:didFinishSavingWithError:contextInfo:);
Uiimagewritetosavedphotosalbum (Theimage, Self,selectortocall, NULL);

}else if ([mediatype isequaltostring: (NSString *) Kuttypemovie]) {
Judging get type: Video
Get the URL of a video file
nsurl* Mediaurl = [info objectforkey:uiimagepickercontrollermediaurl];
Create a Alassetslibrary object and save the video to the Media Library
The Assets Library Framework package is a feature that provides the ability to manipulate pictures and videos in an application. The equivalent of a bridge linking the application and multimedia files.
Alassetslibrary *assetslibrary = [[Alassetslibrary alloc] init];
Save a video to an album
[Assetslibrary Writevideoatpathtosavedphotosalbum:mediaurl
completionblock:^ (Nsurl *asseturl, Nserror *error) {
if (!error) {
NSLog (@ "captured video saved with no error.");
}else{
NSLog (@ "error occured while saving the video:%@", error);
}
}];
[Assetslibrary release];


}


[Picker Dismissmodalviewcontrolleranimated:yes];
}






When the user cancels, the method is called
-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{

[Picker Dismissmodalviewcontrolleranimated:yes];
}


Iv. getting pictures and video data from albums


1. We encapsulate the function in a click event of a button


#pragma mark-Album Action


-(void) Clickshowphotoaction: (ID) sender{

if ([self isphotolibraryavailable]) {
Uiimagepickercontroller *controller = [[Uiimagepickercontroller alloc] init];
[Controller setsourcetype:uiimagepickercontrollersourcetypephotolibrary];//Setup Type
Nsmutablearray *mediatypes = [[Nsmutablearray alloc] init];
if ([self canuserpickphotosfromphotolibrary]) {
[MediaTypes AddObject: ( NSString *) Kuttypeimage];
}
if ([self canuserpickvideosfromphotolibrary]) {
[MediaTypes AddObject: ( NSString *) Kuttypemovie];
}

[Controller setmediatypes:mediatypes];
[Controller setdelegate:self];//Setup Agent
[Self.navigationcontroller Presentmodalviewcontroller:controller Animated:yes];
[Controller release];
[MediaTypes release];

}




}


2. Regarding the Uiimagepickercontrollerdelegate protocol, we can reuse it.


Here, you don't have to repeat it!


Finally, it is necessary to say that in the Uiimagepickercontrollerdelegate protocol
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) The info value in the Info method, depending on the type of operation we have, produces different data information:


When manipulated for pictures::



{
Uiimagepickercontrollercroprect = "Nsrect: {{0, 405}, {2448, 2449}}";
Uiimagepickercontrollereditedimage = "";
Uiimagepickercontrollermediametadata = {
Dpiheight = 72;
Dpiwidth = 72;
Orientation = 6;
' {Exif} ' = {
Aperturevalue = "2.526068811667588";
Brightnessvalue = "-0.0709875088566263";
ColorSpace = 1;
datetimedigitized = "2013:04:05 16:43:00";
datetimeoriginal = "2013:04:05 16:43:00";
Exposuremode = 0;
Exposureprogram = 2;
Exposuretime = "0.05882352941176471";
Fnumber = "2.4";
Flash = 24;
Focallenin35mmfilm = 35;
FocalLength = "4.28";
Isospeedratings = (
400
);
Meteringmode = 5;
Pixelxdimension = 3264;
Pixelydimension = 2448;
Scenetype = 1;
Sensingmethod = 2;
sharpness = 0;
Shutterspeedvalue = "4.099543917546131";
Subjectarea = (
1631,
1223,
881,
881
);
whitebalance = 0;
};
' {TIFF} ' = {
DateTime = "2013:04:05 16:43:00";
make = Apple;
Model = "IPhone 4S";
Software = "5.1.1";
XResolution = 72;
YResolution = 72;
};
};
Uiimagepickercontrollermediatype = "Public.image";
Uiimagepickercontrolleroriginalimage = "";
}


When we are working with video:


{
Uiimagepickercontrollermediatype = "Public.movie";
Uiimagepickercontrollermediaurl = "file://localhost/private/var/mobile/Applications/ 22a14825-dd7e-48e1-a1d5-2d85b82095b5/tmp/capture-t0x1363a0.tmp.etxfd4/capturedvideo. MOV ";
}


Hope to help you!

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.