Uiimagepickercontroller Photography and photography This class inherits from the Uinavigationcontroller class step:Check that the media source mode is availableCheck which media types are supported in this source modeCreate an image selection controller, set its properties, and displayhandling in the delegate agreement method1. Check the media source call the static method of the Uiimagepickercontroller class issourcetypeavailable to check that SourceType is an enumeration value of type Uiimagepickercontrollersourcetype , which represents 3 different media source modes for the image selection controlleruiimagepickercontrollersourcetypephotolibrary: Photo gallery mode. The image selection controller will browse the root directory of the system photo Library when it is displayed in this mode. Uiimagepickercontrollersourcetypecamera: Camera mode, the image selection controller is displayed in this mode can be photographed or video. uiimagepickercontrollersourcetypesavedphotosalbum: Camera roll mode, the image selection controller will browse the camera roll catalog when displayed in this mode. If the device supports the specified media source mode, the Issourcetypeavailable: method returns Yes, otherwise returns No. 2. Check the supported media types call another static method of the Uiimagepickercontroller class Availablemediatypesforsourcetype: Returns an array of strings, Kuttypeimage represents a static picture, Kuttypemovie represents video.These two string constants are defined in the Mobilecoreservices framework. parameter info is a dictionary that contains the media type, the original picture of the photo, the edited image, or the URL of the video file that is being filmed.
//
ViewController.h
Camera
//
Created by Gao Wuhang on 12-11-23.
Copyright (c) 2012 Gao Wuhang. All rights reserved.
//
#import
@interface Viewcontroller:uiviewcontroller<</span>uinavigationcontrollerdelegate, Uiimagepickercontrollerdelegate>
-(Ibaction) Takepicturebuttonclick: (ID) sender;
-(Ibaction) Capturevideobuttonclick: (ID) sender;
@end
//
Viewcontroller.m
Camera
//
Created by Gao Wuhang on 12-11-23.
Copyright (c) 2012 Gao Wuhang. All rights reserved.
//
#import "ViewController.h"
#import
#import
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(Ibaction) Takepicturebuttonclick: (ID) sender{
Check if camera mode is available
if (![ Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {
NSLog (@ "Sorry, no camera or camera is unavailable.");
Return
}
Get media types supported in camera mode
nsarray* availablemediatypes = [Uiimagepickercontroller availablemediatypesforsourcetype: Uiimagepickercontrollersourcetypecamera];
BOOL cantakepicture = NO;
For (nsstring* mediatype in availablemediatypes) {
if ([MediaType isequaltostring: (nsstring*) kuttypeimage]) {
Support Photography
Cantakepicture = YES;
Break
}
}
Check if photo support is supported
if (!cantakepicture) {
NSLog (@ "Sorry, taking picture was not supported.");
Return
}
Create an Image selection controller
uiimagepickercontroller* Imagepickercontroller = [[Uiimagepickercontroller alloc] init];
Set the source mode of the image selection controller to camera mode
Imagepickercontroller.sourcetype = Uiimagepickercontrollersourcetypecamera;
Set the image selection controller type to a static image
Imagepickercontroller.mediatypes = [[[Nsarray Alloc] initwithobjects: (nsstring*) kuttypeimage, nil] autorelease];
Allow user to edit
imagepickercontroller.allowsediting = YES;
Set Delegate Object
Imagepickercontroller.delegate = self;
Display in the form of a modular view controller
[Self Presentmodalviewcontroller:imagepickercontroller animated:yes];
[Imagepickercontroller release];
}
-(Ibaction) Capturevideobuttonclick: (ID) sender{
Check if camera mode is available
if (![ Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {
NSLog (@ "Sorry, no camera or camera is unavailable!!!");
Return
}
Get media types supported in camera mode
nsarray* availablemediatypes = [Uiimagepickercontroller availablemediatypesforsourcetype: Uiimagepickercontrollersourcetypecamera];
BOOL cantakevideo = NO;
For (nsstring* mediatype in availablemediatypes) {
if ([MediaType isequaltostring: (NSString *) Kuttypeimage]) {
Support Camera
Cantakevideo = YES;
Break
}
}
Check if camera support is supported
if (!cantakevideo) {
NSLog (@ "Sorry, capturing video is not supported.!!!");
Return
}
Create an Image selection controller
uiimagepickercontroller* Imagepickercontroller = [[Uiimagepickercontroller alloc] init];
Set the source mode of the image selection controller to camera mode
Imagepickercontroller.sourcetype = Uiimagepickercontrollersourcetypecamera;
Set the image selection controller's type as a dynamic image
Imagepickercontroller.mediatypes = [[[Nsarray Alloc] initwithobjects: (nsstring*) Kuttypemovie, nil] autorelease];
Set Video image quality
imagepickercontroller.videoquality = Uiimagepickercontrollerqualitytypehigh;
Setting the maximum camera time
Imagepickercontroller.videomaximumduration = 30;
Allow user to edit
imagepickercontroller.allowsediting = YES;
Set Delegate Object
Imagepickercontroller.delegate = self;
Display as a mode view controller
[Self Presentmodalviewcontroller:imagepickercontroller animated:yes];
[Imagepickercontroller release];
}
-(void) Image: (uiimage*) Image didfinishsavingwitherror: (nserror*) Error ContextInfo: (void*) contextinfo{
if (!error) {
NSLog (@ "Picture saved with no error.");
}
Else
{
NSLog (@ "error occured while saving the picture%@", error);
}
}
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info {
Print out the contents of the dictionary
NSLog (@ "Get the media info:%@", info);
Get Media type
nsstring* mediatype = [info Objectforkey:uiimagepickercontrollermediatype];
Determine whether it is a static image or a video
if ([MediaType isequaltostring: (NSString *) Kuttypeimage]) {
Get the image after the user edits
uiimage* editedimage = [info objectforkey:uiimagepickercontrollereditedimage];
Save the image to the Media Library
Uiimagewritetosavedphotosalbum (Editedimage, Self, @selector (image:didFinishSavingWithError:contextInfo:), NULL);
}else if ([mediatype isequaltostring: (NSString *) Kuttypemovie])
{
Get the URL of a video file
nsurl* Mediaurl = [info objectforkey:uiimagepickercontrollermediaurl];
Create a Alassetslibrary object and save the video to the Media Library
alassetslibrary* assetslibrary = [[Alassetslibrary alloc] init];
[Assetslibrary writevideoatpathtosavedphotosalbum:mediaurlcompletionblock:^ (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];
}
-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{
[Picker Dismissmodalviewcontrolleranimated:yes];
}
-(void) viewdidload
{
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
}
-(void) viewdidunload
{
[Super Viewdidunload];
Release any retained subviews of the main view.
}
-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
Return (interfaceorientation! = Uiinterfaceorientationportraitupsidedown);
}
@end
IOS Uiimagepickercontroller photo and video