15.0~15.8 摄像头 照片 视频

该博客介绍了如何在iOS应用中检测和使用摄像头,包括检测摄像头可用性、支持的媒体类型,以及用户拍照和录制视频的功能。通过UIImagePickerController,可以访问照片库,并使用Assets Library framework来处理拍摄的内容。同时,还展示了如何编辑已拍摄的视频。
摘要由CSDN通过智能技术生成

15.0. IntroductionCamera and the Photo Library

大部分的iOS设别都有摄像头,UIImagePickerController可以让你显示熟悉的拍照录像界面。这一章让你学会如何让用户拍照,录像,以及如何访问拍照录像的内容。

首先,程序需要知道有没有摄像头,让不让程序用。所以你需要引入MobileCoreServices.framework 

访问图片会比较简单,因为我们可以直接得到图片的地址

对于视频,我们只能拿到类似这样的:assets-library://asset/asset.MOV?id=1000000004&ext=MOV

我们需要用Assets Library framework来访问视频,当然,我们也可以用它来保持图片和视频,这样别的应用就可以访问到我们保存的资源。

编译本章例子前,请确保MobileCoreServices.frameworkAssets 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值