你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS攝像頭和相冊-UIImagePickerController-淺析

iOS攝像頭和相冊-UIImagePickerController-淺析

編輯:IOS開發綜合
 


在一些應用中,我們需要用到iOS設備的攝像頭進行拍照,視頻。並且從相冊中選取我們需要的圖片或者視頻。
關於iOS攝像頭和相冊的應用,可以使用UIImagePickerController類來完成控制。
關於UIImagePickerController的相關知識,
如下:


iOS的一些設備上都安裝了攝像頭。現在絕大多數都有了。
在編程中,我們是用相應的東西來進行照相,錄像等功能。


一、UIImagePickerController類
UIImagePickerController 這個類可以為大家提供照相的功能,以及圖片,視頻浏覽的功能。


二、檢查硬件是否安裝有攝像頭或者允許操作相冊


這些公共的方法,我們也許會用到,我就貼了!So easy!!!


#pragma mark - 攝像頭和相冊相關的公共類


// 判斷設備是否有攝像頭
- (BOOL) isCameraAvailable{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}


// 前面的攝像頭是否可用
- (BOOL) isFrontCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}


// 後面的攝像頭是否可用
- (BOOL) isRearCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}




// 判斷是否支持某種多媒體類型:拍照,視頻
- (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;
}


// 檢查攝像頭是否支持錄像
- (BOOL) doesCameraSupportShootingVideos{
return [self cameraSupportsMedia:( NSString *)kUTTypeMoviesourceType:UIImagePickerControllerSourceTypeCamera];
}


// 檢查攝像頭是否支持拍照
- (BOOL) doesCameraSupportTakingPhotos{
return [self cameraSupportsMedia:( NSString *)kUTTypeImagesourceType:UIImagePickerControllerSourceTypeCamera];
}


#pragma mark - 相冊文件選取相關
// 相冊是否可用
- (BOOL) isPhotoLibraryAvailable{
return [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary];
}


// 是否可以在相冊中選擇視頻
- (BOOL) canUserPickVideosFromPhotoLibrary{
return [self cameraSupportsMedia:( NSString *)kUTTypeMovie sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}


// 是否可以在相冊中選擇視頻
- (BOOL) canUserPickPhotosFromPhotoLibrary{
return [self cameraSupportsMedia:( NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}


三、用攝像頭進行拍照和錄像功能


1.我們將UIImagePickerController功能寫在一個按鈕的點擊事件中:



#pragma mark - 拍照按鈕事件


- (void)ClickControlAction:(id)sender{
// 判斷有攝像頭,並且支持拍照功能
if ([self isCameraAvailable] && [self doesCameraSupportTakingPhotos]){
// 初始化圖片選擇控制器
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setSourceType:UIImagePickerControllerSourceTypeCamera];// 設置類型



// 設置所支持的類型,設置只能拍照,或則只能錄像,或者兩者都可以
NSString *requiredMediaType = ( NSString *)kUTTypeImage;
NSString *requiredMediaType1 = ( NSString *)kUTTypeMovie;
NSArray *arrMediaTypes=[NSArray arrayWithObjects:requiredMediaType, requiredMediaType1,nil];
[controller setMediaTypes:arrMediaTypes];

// 設置錄制視頻的質量
[controller setVideoQuality:UIImagePickerControllerQualityTypeHigh];
//設置最長攝像時間
[controller setVideoMaximumDuration:10.f];



[controller setAllowsEditing:YES];// 設置是否可以管理已經存在的圖片或者視頻
[controller setDelegate:self];// 設置代理
[self.navigationController presentModalViewController:controller animated:YES];
[controller release];
} else {
NSLog(@Camera is not available.);
}
}




解釋:


2. setSourceType方法


通過設置setSourceType方法可以確定調用出來的UIImagePickerController所顯示出來的界面


typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
};


分別表示:圖片列表,攝像頭,相機相冊


3.setMediaTypes方法


// 設置所支持的類型,設置只能拍照,或則只能錄像,或者兩者都可以
NSString *requiredMediaType = ( NSString *)kUTTypeImage;
NSString *requiredMediaType1 = ( NSString *)kUTTypeMovie;
NSArray *arrMediaTypes=[NSArray arrayWithObjects:requiredMediaType, requiredMediaType1,nil];
[controller setMediaTypes:arrMediaTypes];




4.關於UIImagePickerControllerDelegate協議


我們要對我們拍攝的照片和視頻進行存儲,那麼就要實現UIImagePickerControllerDelegate協議的方法。


#pragma mark - UIImagePickerControllerDelegate 代理方法




// 保存圖片後到相冊後,調用的相關方法,查看是否保存成功
- (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);
}
}


// 當得到照片或者視頻後,調用該方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@Picker returned successfully.);
NSLog(@%@, info);
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
// 判斷獲取類型:圖片
if ([mediaType isEqualToString:( NSString *)kUTTypeImage]){
UIImage *theImage = nil;
// 判斷,圖片是否允許修改
if ([picker allowsEditing]){
//獲取用戶編輯之後的圖像
theImage = [info objectForKey:UIImagePickerControllerEditedImage];
} else {
// 照片的元數據參數
theImage = [info objectForKey:UIImagePickerControllerOriginalImage];

}

// 保存圖片到相冊中
SEL selectorToCall = @selector(imageWasSavedSuccessfully:didFinishSavingWithError:contextInfo:);
UIImageWriteToSavedPhotosAlbum(theImage, self,selectorToCall, NULL);

}else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){
// 判斷獲取類型:視頻
//獲取視頻文件的url
NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
//創建ALAssetsLibrary對象並將視頻保存到媒體庫
// Assets Library 框架包是提供了在應用程序中操作圖片和視頻的相關功能。相當於一個橋梁,鏈接了應用程序和多媒體文件。
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];
}


四、從相冊獲取圖片和視頻數據


1.我們將功能封裝在一個按鈕的點擊事件中


#pragma mark - 相冊操作


- (void)ClickShowPhotoAction:(id)sender{

if ([self isPhotoLibraryAvailable]){
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];// 設置類型
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];// 設置代理
[self.navigationController presentModalViewController:controller animated:YES];
[controller release];
[mediaTypes release];

}




}


2.關於UIImagePickerControllerDelegate協議,我們可以重用。


在這裡,就不用贅述了!


最後,需要說的是,UIImagePickerControllerDelegate協議中
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法,中的info值,會根據我們操作的類型不同,而產生了不同的數據信息:


當操作的為圖片時::



{
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 = ;
}


當我們操作的為視頻時:


{
UIImagePickerControllerMediaType = public.movie;
UIImagePickerControllerMediaURL = file://localhost/private/var/mobile/Applications/22A14825-DD7E-48E1-A1D5-2D85B82095B5/tmp/capture-T0x1363a0.tmp.etXfD4/capturedvideo.MOV;
}


希望對你有所幫助!
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved