該類繼承自UINavigationController類
//
// 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{
//檢查相機模式是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"sorry, no camera or camera is unavailable.");
return;
}
//獲得相機模式下支持的媒體類型
NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
BOOL canTakePicture = NO;
for (NSString* mediaType in availableMediaTypes) {
if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
//支持拍照
canTakePicture = YES;
break;
}
}
//檢查是否支持拍照
if (!canTakePicture) {
NSLog(@"sorry, taking picture is not supported.");
return;
}
//創建圖像選取控制器
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
//設置圖像選取控制器的來源模式為相機模式
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//設置圖像選取控制器的類型為靜態圖像
imagePickerController.mediaTypes = [[[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage, nil] autorelease];
//允許用戶進行編輯
imagePickerController.allowsEditing = YES;
//設置委托對象
imagePickerController.delegate = self;
//以模視圖控制器的形式顯示
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
- (IBAction)captureVideoButtonClick:(id)sender{
//檢查相機模式是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"sorry, no camera or camera is unavailable!!!");
return;
}
//獲得相機模式下支持的媒體類型
NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
BOOL canTakeVideo = NO;
for (NSString* mediaType in availableMediaTypes) {
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//支持攝像
canTakeVideo = YES;
break;
}
}
//檢查是否支持攝像
if (!canTakeVideo) {
NSLog(@"sorry, capturing video is not supported.!!!");
return;
}
//創建圖像選取控制器
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
//設置圖像選取控制器的來源模式為相機模式
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//設置圖像選取控制器的類型為動態圖像
imagePickerController.mediaTypes = [[[NSArray alloc] initWithObjects:(NSString*)kUTTypeMovie, nil] autorelease];
//設置攝像圖像品質
imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
//設置最長攝像時間
imagePickerController.videoMaximumDuration = 30;
//允許用戶進行編輯
imagePickerController.allowsEditing = YES;
//設置委托對象
imagePickerController.delegate = self;
//以模式視圖控制器的形式顯示
[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{
//打印出字典中的內容
NSLog(@"get the media info: %@", info);
//獲取媒體類型
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//判斷是靜態圖像還是視頻
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//獲取用戶編輯之后的圖像
UIImage* editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
//將該圖像保存到媒體庫中
UIImageWriteToSavedPhotosAlbum(editedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
//獲取視頻文件的url
NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
//創建ALAssetsLibrary對象並將視頻保存到媒體庫
ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
[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];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any 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