15.0. Introduction(Camera and the Photo Library)
大部分的iOS设别都有摄像头,UIImagePickerController可以让你显示熟悉的拍照录像界面。这一章让你学会如何让用户拍照,录像,以及如何访问拍照录像的内容。
首先,程序需要知道有没有摄像头,让不让程序用。所以你需要引入MobileCoreServices.framework
访问图片会比较简单,因为我们可以直接得到图片的地址
对于视频,我们只能拿到类似这样的:assets-library://asset/asset.MOV?id=1000000004&ext=MOV
我们需要用Assets Library framework来访问视频,当然,我们也可以用它来保持图片和视频,这样别的应用就可以访问到我们保存的资源。
编译本章例子前,请确保MobileCoreServices.framework和Assets Library framework 两个框架,你已经添加到应用中。并引用了头文件
#import <MobileCoreServices/MobileCoreServices.h>
#import <AssetsLibrary/AssetsLibrary.h>
要通过url访问asset数据,参照以下步骤
1,创建ALAssetsLibrary对象
2,用 assetForURL:resultBlock:failureBlock 来访问
3,释放刚创建的对象
15.1. Detecting and Probing the Camera
检测摄像头
检查camera是否可用
-(void)testisCameraAvailable
{
if ([selfisCameraAvailable]){
NSLog(@"Camera is available.");
} else {
NSLog(@"Camera is not available.");
}
}
- (BOOL) isCameraAvailable{
return [UIImagePickerControllerisSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera];
}
真机打印:
2014-07-07 16:39:44.366 cookbook7_12[1064:907] Camera is available.
模拟器打印:
2014-07-07 16:40:38.642 cookbook7_12[748:c07] Camera is not available.
typedefNS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
};
检查是否支持媒体类型
-(void)testcameraSupport
{
if ([selfdoesCameraSupportTakingPhotos]){
NSLog(@"The camera supports taking photos.");
} else {
NSLog(@"The camera does not support taking photos");
}
if ([selfdoesCameraSupportShootingVideos]){
NSLog(@"The camera supports shooting videos.");
} else {
NSLog(@"The camera does not support shooting videos.");
}
}
- (BOOL)cameraSupportsMedia:(NSString *)paramMediaType
sourceType:(UIImagePickerControllerSourceType)paramSourceType{
__block BOOL result = NO;
if ([paramMediaType length] == 0){
NSLog(@"Media type is empty.");
return NO;
}
NSArray *availableMediaTypes =
[UIImagePickerController
availableMediaTypesForSourceType:paramSourceType];
[availableMediaTypesenumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop) {
NSString *mediaType = (NSString *)obj;
if ([mediaType isEqualToString:paramMediaType]){
result =YES;
*stop=YES;
}
}];
return result;
}
- (BOOL) doesCameraSupportShootingVideos{
return [selfcameraSupportsMedia:(__bridgeNSString *)kUTTypeMovie
sourceType:UIImagePickerControllerSourceTypeCamera];
}
- (BOOL) doesCameraSupportTakingPhotos{
return [selfcameraSupportsMedia:(__bridgeNSString *)kUTTypeImage
sourceType:UIImagePickerControllerSourceTypeCamera];
}
真机打印:
2014-07-07 17:22:18.920 cookbook7_12[1088:907] The camera supports taking photos.
2014-07-07 17:22:18.925 cookbook7_12[1088:907] The camera supports shooting videos.
模拟器打印:
2014-07-07 17:28:24.229 cookbook7_12[816:c07] The camera does not support taking photos
2014-07-07 17:28:24.232 cookbook7_12[816:c07] The camera does not support shooting videos.
检查前后摄像头是否可用,Flash是否可用
-(void)testFrontRearCameraAndFlash
{
if ([selfisFrontCameraAvailable]){
NSLog(@"The front camera is available.");
if ([selfisFlashAvailableOnFrontCamera]){
NSLog(@"The front camera is equipped with a flash");
} else {
NSLog(@"The front camera is not equipped with a flash");
}
} else {
NSLog(@"The front camera is not available.");
}
if ([selfisRearCameraAvailable]){
NSLog(@"The rear camera is available.");
if ([selfisFlashAvailableOnRearCamera]){
NSLog(@"The rear camera is equipped with a flash");
} else {
NSLog(@"The rear camera is not equipped with a flash");
}
} else {
NSLog(@"The rear camera is not available.");
}
if ([selfdoesCameraSupportTakingPhotos]){
NSLog(@"The camera supports taking photos.");
} else {
NSLog(@"The camera does not support taking photos");
}
if ([selfdoesCameraSupportShootingVideos]){
NSLog(@"The camera supports shooting videos.");
} else {
NSLog(@"The camera does not support shooting videos.");
}
}
- (BOOL) isFrontCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}
- (BOOL) isRearCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}
- (BOOL) isFlashAvailableOnFrontCamera{
return [UIImagePickerControllerisFlashAvailableForCameraDevice: UIImagePickerControllerCameraDeviceFront];
}
- (BOOL) isFlashAvailableOnRearCamera{
return [UIImagePickerControllerisFlashAvailableForCameraDevice: UIImagePickerControllerCameraDeviceRear];
}
真机打印:
2014-07-07 17:36:18.444 cookbook7_12[1105:907] The front camera is available.
2014-07-07 17:36:18.449 cookbook7_12[1105:907] The front camera is not equipped with a flash
2014-07-07 17:36:18.451 cookbook7_12[1105:907] The rear camera is available.
2014-07-07 17:36:18.452 cookbook7_12[1105:907] The rear camera is equipped with a flash
2014-07-07 17:36:18.454 cookbook7_12[1105:907] The camera supports taking photos.
2014-07-07 17:36:18.455 cookbook7_12[1105:907] The camera supports shooting videos.
模拟器打印:
2014-07-07 17:34:53.856 cookbook7_12[865:c07] The front camera is not available.
2014-07-07 17:34:53.858 cookbook7_12[865:c07] The rear camera is not available.
2014-07-07 17:34:53.859 cookbook7_12[865:c07] The camera does not support taking photos
2014-07-07 17:34:53.860 cookbook7_12[865:c07] The camera does not support shooting videos.
15.2. Taking Photos with the Camera
用户拍照并获取拍到的照片
-(void)testPresentViewController
{
// static BOOL beenHereBefore = NO;
// if (beenHereBefore){
// /* Only display the picker once as the viewDidAppear: method gets
// called whenever the view of our view controller gets displayed */
// return;
// } else {
// beenHereBefore = YES;
// }
if ([selfisCameraAvailable] &&
[selfdoesCameraSupportTakingPhotos]){
UIImagePickerController *controller =[[UIImagePickerControlleralloc] init];
controller.sourceType =UIImagePickerControllerSourceTypeCamera;
NSString *requiredMediaType = (__bridgeNSString *)kUTTypeImage;
// requiredMediaType = (__bridge NSString*)kUTTypeMovie;
controller.mediaTypes = [[NSArrayalloc] initWithObjects:requiredMediaType,nil];
controller.allowsEditing =YES;
controller.delegate =self;
[selfpresentViewController:controller animated:NOcompletion:nil];
} else {
NSLog(@"Camera is not available.");
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"Picker returned successfully.");
NSLog(@"%@", info);
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(__bridgeNSString *)kUTTypeMovie]){
NSURL *urlOfVideo = info[UIImagePickerControllerMediaURL];
NSLog(@"Video URL = %@", urlOfVideo);
}
else if ([mediaType isEqualToString:(__bridge NSString *)kUTTypeImage]){
/* Let's get the metadata. This is only for
images. Not videos */
NSDictionary *metadata = info[UIImagePickerControllerMediaMetadata];
UIImage *theImage = info[UIImagePickerControllerOriginalImage];
NSLog(@"Image Metadata = %@", metadata);
NSLog(@"Image = %@", theImage);
id cropRect = info[UIImagePickerControllerCropRect];
id editedImage = info[UIImagePickerControllerEditedImage];
NSLog(@"cropRect = %@",cropRect);
NSLog(@"editedImage = %@",editedImage);
}
[picker dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"Picker was cancelled");
[picker dismissViewControllerAnimated:YES