UIImagePickerController拍照與攝像(轉)


該類繼承自UINavigationController類

步驟:
檢查媒體來源模式是否可用
檢查該來源模式下所支持的媒體類型
創建圖像選取控制器,設置其屬性並顯示
在委托協議方法中處理
 
1.檢查媒體來源
調用UIImagePickerController類的靜態方法isSourceTypeAvailable來檢查
sourceType是一個UIImagePickerControllerSourceType類型的枚舉值,它表示圖像選取控制器的3種不同的媒體來源模式
UIImagePickerControllerSourceTypePhotoLibrary:照片庫模式。圖像選取控制器以該模式顯示時會瀏覽系統照片庫的根目錄。
UIImagePickerControllerSourceTypeCamera:相機模式,圖像選取控制器以該模式顯示時可以進行拍照或攝像。
UIImagePickerControllerSourceTypeSavedPhotosAlbum:相機膠卷模式,圖像選取控制器以該模式顯示時會瀏覽相機膠卷目錄。
如果設備支持指定的媒體來源模式,則isSourceTypeAvailable:方法返回YES,否則返回NO。
2.檢查支持的媒體類型
調用UIImagePickerController類的另一個靜態方法 availableMediaTypesForSourceType:
返回的是字符串數組,kUTTypeImage表示靜態圖片,kUTTypeMovie表示視頻。 這兩個字符串常量定義在MobileCoreServices框架中。
 
參數info是一個字典,包含媒體類型,拍照的原始圖片,編輯后的圖片,或是攝像的視頻文件的URL等。

//

//  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

 
參考:http://blog.csdn.net/pucker


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM