15.0~15.8 摄像头 照片 视频

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:YEScompletion:nil];

}




拍照返回打印:

2014-07-08 11:56:18.865 cookbook7_12[2073:907] Picker returned successfully.

2014-07-08 11:56:18.869 cookbook7_12[2073:907] {

    UIImagePickerControllerCropRect = "NSRect: {{703, 1248}, {1052, 1053}}";

    UIImagePickerControllerEditedImage = "<UIImage: 0x20083bb0>";

    UIImagePickerControllerMediaMetadata =     {

        DPIHeight = 72;

        DPIWidth = 72;

        Orientation = 6;

        "{Exif}" =         {

            ApertureValue = "2.526068811667588";

            BrightnessValue = "1.606492604207528";

            ColorSpace = 1;

            DateTimeDigitized = "2014:07:08 11:56:14";

            DateTimeOriginal = "2014:07:08 11:56:14";

            ExposureMode = 0;

            ExposureProgram = 2;

            ExposureTime = "0.05";

            FNumber = "2.4";

            Flash = 24;

            FocalLenIn35mmFilm = 35;

            FocalLength = "4.28";

            ISOSpeedRatings =             (

                250

            );

            MeteringMode = 5;

            PixelXDimension = 3264;

            PixelYDimension = 2448;

            SceneType = 1;

            SensingMethod = 2;

            ShutterSpeedValue = "4.321928094887363";

            SubjectArea =             (

                1631,

                1223,

                881,

                881

            );

            WhiteBalance = 0;

        };

        "{TIFF}" =         {

            DateTime = "2014:07:08 11:56:14";

            Make = Apple;

            Model = "iPhone 4S";

            Software = "6.0.1";

            XResolution = 72;

            YResolution = 72;

        };

    };

    UIImagePickerControllerMediaType = "public.image";

    UIImagePickerControllerOriginalImage = "<UIImage: 0x20083be0>";

}

2014-07-08 11:56:18.877 cookbook7_12[2073:907] Image Metadata = {

    DPIHeight = 72;

    DPIWidth = 72;

    Orientation = 6;

    "{Exif}" =     {

        ApertureValue = "2.526068811667588";

        BrightnessValue = "1.606492604207528";

        ColorSpace = 1;

        DateTimeDigitized = "2014:07:08 11:56:14";

        DateTimeOriginal = "2014:07:08 11:56:14";

        ExposureMode = 0;

        ExposureProgram = 2;

        ExposureTime = "0.05";

        FNumber = "2.4";

        Flash = 24;

        FocalLenIn35mmFilm = 35;

        FocalLength = "4.28";

        ISOSpeedRatings =         (

            250

        );

        MeteringMode = 5;

        PixelXDimension = 3264;

        PixelYDimension = 2448;

        SceneType = 1;

        SensingMethod = 2;

        ShutterSpeedValue = "4.321928094887363";

        SubjectArea =         (

            1631,

            1223,

            881,

            881

        );

        WhiteBalance = 0;

    };

    "{TIFF}" =     {

        DateTime = "2014:07:08 11:56:14";

        Make = Apple;

        Model = "iPhone 4S";

        Software = "6.0.1";

        XResolution = 72;

        YResolution = 72;

    };

}

2014-07-08 11:56:18.882 cookbook7_12[2073:907] Image = <UIImage: 0x20083be0>

2014-07-08 11:56:18.884 cookbook7_12[2073:907] cropRect = NSRect: {{703, 1248}, {1052, 1053}}

2014-07-08 11:56:18.885 cookbook7_12[2073:907] editedImage = <UIImage: 0x20083bb0>




取消返回打印:

2014-07-08 10:07:09.082 cookbook7_12[1962:907] Picker was cancelled




修改一下拍摄类型

        NSString *requiredMediaType = (__bridgeNSString *) kUTTypeMovie;


拍完返回打印:

2014-07-08 10:29:20.448 cookbook7_12[1996:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-08 10:29:20.450 cookbook7_12[1996:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-08 10:29:20.464 cookbook7_12[1996:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-08 10:29:20.490 cookbook7_12[1996:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0

2014-07-08 10:29:20.906 cookbook7_12[1996:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-08 10:29:20.913 cookbook7_12[1996:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1

2014-07-08 10:29:22.157 cookbook7_12[1996:907] Picker returned successfully.

2014-07-08 10:29:22.158 cookbook7_12[1996:907] {

    UIImagePickerControllerMediaType = "public.movie";

    UIImagePickerControllerMediaURL = "file://localhost/private/var/mobile/Applications/19E5F719-0326-4788-B127-E618CC0C6523/tmp/capture/capturedvideo.MOV";

}

2014-07-08 10:29:22.160 cookbook7_12[1996:907] Video URL = file://localhost/private/var/mobile/Applications/19E5F719-0326-4788-B127-E618CC0C6523/tmp/capture/capturedvideo.MOV



拍照返回Image图片对象,视频返回文件地址


UIImagePickerControllerMediaMetadata  //NSDictionary图片的一些相关信息

UIImagePickerControllerOriginalImage //UIImage 图片数据

UIImagePickerControllerCropRect //如果allowsEditing =YES  这将返回剪切的区域

UIImagePickerControllerEditedImage //返回剪切出来的图片



 


15.3. Taking Videos with the Camera

修改一下拍摄类型就可以录像,看15.2

        NSString *requiredMediaType = (__bridgeNSString *) kUTTypeMovie;


拍摄的视频保存在,应用目录下的临时文件夹中


录像设置

videoQuality  录像质量设置UIImagePickerControllerQualityTypeHigh or UIImagePickerControllerQuali tyTypeMedium

videoMaximumDuration 最大录像时间,单位:秒


比如要拍摄一段30秒的高清视频,可以这样




-(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;

        controller.mediaTypes =@[(__bridgeNSString *)kUTTypeMovie];

        controller.allowsEditing =YES;

        controller.delegate =self;

        /* Record in high quality */

        controller.videoQuality =UIImagePickerControllerQualityTypeHigh;

        /* Only allow 30 seconds of recording */

        controller.videoMaximumDuration =30.0f;

        [selfpresentViewController:controller animated:YEScompletion:nil];

    } else {

        NSLog(@"Camera is not available.");

    }

}


打印:

2014-07-08 14:13:03.435 cookbook7_12[2147:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-08 14:13:03.442 cookbook7_12[2147:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-08 14:13:03.470 cookbook7_12[2147:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-08 14:13:03.551 cookbook7_12[2147:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0

2014-07-08 14:13:04.139 cookbook7_12[2147:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-08 14:13:04.146 cookbook7_12[2147:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1

2014-07-08 14:13:18.658 cookbook7_12[2147:907] Picker returned successfully.

2014-07-08 14:13:18.659 cookbook7_12[2147:907] {

    UIImagePickerControllerMediaType = "public.movie";

    UIImagePickerControllerMediaURL = "file://localhost/private/var/mobile/Applications/19E5F719-0326-4788-B127-E618CC0C6523/tmp/capture-T0x1fd45fd0.tmp.LDHQ8y/capturedvideo.MOV";

}

2014-07-08 14:13:18.660 cookbook7_12[2147:907] Video URL = file://localhost/private/var/mobile/Applications/19E5F719-0326-4788-B127-E618CC0C6523/tmp/capture-T0x1fd45fd0.tmp.LDHQ8y/capturedvideo.MOV




15.4. Storing Photos in the Photo Library

保存照片到图库



-(void)testSaveImageToPhotoLibrary

{

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

        controller.mediaTypes =@[(__bridgeNSString *)kUTTypeImage];

        controller.allowsEditing =YES;

        controller.delegate =self;

        [selfpresentViewController:controller animated:YEScompletion:nil];

    } else {

        NSLog(@"Camera is not available.");

    }

}


- (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[UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(__bridgeNSString *)kUTTypeImage]){

        UIImage *theImage = nil;

        if ([picker allowsEditing]){

            theImage = info[UIImagePickerControllerEditedImage];

        } else {

            theImage = info[UIImagePickerControllerOriginalImage];

        }

        SEL selectorToCall = @selector(imageWasSavedSuccessfully:didFinishSavingWithError:\

                                       contextInfo:);

        UIImageWriteToSavedPhotosAlbum(theImage,

                                      self,

                                       selectorToCall,

                                      NULL);

    }

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}



打印:

2014-07-08 14:22:09.953 cookbook7_12[2191:907] Picker returned successfully.

2014-07-08 14:22:09.958 cookbook7_12[2191:907] {

    UIImagePickerControllerCropRect = "NSRect: {{0, 405}, {2448, 2449}}";

    UIImagePickerControllerEditedImage = "<UIImage: 0x1d5b4ca0>";

    UIImagePickerControllerMediaMetadata =     {

        DPIHeight = 72;

        DPIWidth = 72;

        Orientation = 6;

        "{Exif}" =         {

            ApertureValue = "2.526068811667588";

            BrightnessValue = "1.405407921370805";

            ColorSpace = 1;

            DateTimeDigitized = "2014:07:08 14:22:02";

            DateTimeOriginal = "2014:07:08 14:22:02";

            ExposureMode = 0;

            ExposureProgram = 2;

            ExposureTime = "0.05";

            FNumber = "2.4";

            Flash = 24;

            FocalLenIn35mmFilm = 35;

            FocalLength = "4.28";

            ISOSpeedRatings =             (

                250

            );

            MeteringMode = 5;

            PixelXDimension = 3264;

            PixelYDimension = 2448;

            SceneType = 1;

            SensingMethod = 2;

            ShutterSpeedValue = "4.321928094887363";

            SubjectArea =             (

                1631,

                1223,

                881,

                881

            );

            WhiteBalance = 0;

        };

        "{TIFF}" =         {

            DateTime = "2014:07:08 14:22:02";

            Make = Apple;

            Model = "iPhone 4S";

            Software = "6.0.1";

            XResolution = 72;

            YResolution = 72;

        };

    };

    UIImagePickerControllerMediaType = "public.image";

    UIImagePickerControllerOriginalImage = "<UIImage: 0x1d5b4cd0>";

}

2014-07-08 14:22:25.923 cookbook7_12[2191:907] Image was saved successfully.









UIImageWriteToSavedPhotosAlbum 的参数

1,the image

2, 保存后通知这个对象

3,保存后调用步骤2对象的这个方法

4,步骤3方法的参数


当然了234也可以没有


当第一次要求保存时,会咨询用户的同意



15.5. Storing Videos in the Photo Library

保存视频




-(void)testStoreVideo

{

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

        controller.mediaTypes =@[(__bridgeNSString *)kUTTypeMovie];

        controller.allowsEditing =YES;

        controller.delegate =self;

        [selfpresentViewController:controller animated:YEScompletion: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);

        

        

        ALAssetsLibrary *assetsLibrary;

        assetsLibrary = [[ALAssetsLibraryalloc] init];

//        NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"MyVideo"

//                                                  withExtension:@"MOV"];

        NSURL *videoURL = urlOfVideo;

        if (videoURL != nil){

            [assetsLibrarywriteVideoAtPathToSavedPhotosAlbum:videoURL

                                             completionBlock:^(NSURL *assetURL,NSError *error) {

                                                 if (error == nil){

                                                     NSLog(@"no errors happened");

                                                     NSLog(@"assetURL=%@",assetURL);//注意这里的结果

                                                  }else {

                                                     NSLog(@"Error happened while saving the video.");

                                                     NSLog(@"The error is = %@", error);

                                                  }

                                              }];

        } else {

            NSLog(@"Could not find the video in the app bundle.");

        }

        

        

    }else{

        NSLog(@"mediaType is not kUTTypeMovie");

    }


    [picker dismissViewControllerAnimated:YEScompletion:nil];

}

打印:


2014-07-08 16:34:22.485 cookbook7_12[2297:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-08 16:34:22.486 cookbook7_12[2297:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-08 16:34:22.504 cookbook7_12[2297:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-08 16:34:22.581 cookbook7_12[2297:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0

2014-07-08 16:34:23.036 cookbook7_12[2297:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-08 16:34:23.043 cookbook7_12[2297:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1

2014-07-08 16:34:24.384 cookbook7_12[2297:907] Picker returned successfully.

2014-07-08 16:34:24.385 cookbook7_12[2297:907] {

    UIImagePickerControllerMediaType = "public.movie";

    UIImagePickerControllerMediaURL = "file://localhost/private/var/mobile/Applications/19E5F719-0326-4788-B127-E618CC0C6523/tmp/capture-T0x1f55ab60.tmp.oecicD/capturedvideo.MOV";

}

2014-07-08 16:34:24.386 cookbook7_12[2297:907] Video URL = file://localhost/private/var/mobile/Applications/19E5F719-0326-4788-B127-E618CC0C6523/tmp/capture-T0x1f55ab60.tmp.oecicD/capturedvideo.MOV

2014-07-08 16:34:24.885 cookbook7_12[2297:907] no errors happened

2014-07-08 16:34:24.886 cookbook7_12[2297:907] assetURL=assets-library://asset/asset.MOV?id=86E60C71-CEFD-4F98-A7B6-0DA40E04D5FA&ext=MOV


相机胶卷里面也看到了刚才拍摄的视频

 


15.6. Retrieving Photos and Videos from the Photo Library

从照片库中选择照片,视频文件



-(void)testPickPhone

{

//    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 ([selfisPhotoLibraryAvailable]){

        UIImagePickerController *controller =[[UIImagePickerControlleralloc] init];

        controller.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

        NSMutableArray *mediaTypes = [[NSMutableArrayalloc] init];

        if ([selfcanUserPickPhotosFromPhotoLibrary]){

            [mediaTypesaddObject:(__bridgeNSString *)kUTTypeImage];

        }

        if ([selfcanUserPickVideosFromPhotoLibrary]){

            [mediaTypesaddObject:(__bridgeNSString *)kUTTypeMovie];

        }

        controller.mediaTypes = mediaTypes;

        controller.delegate =self;

        [selfpresentViewController:controller animated:YEScompletion:nil];

    }

}



- (BOOL) isPhotoLibraryAvailable{

    return [UIImagePickerControllerisSourceTypeAvailable:

            UIImagePickerControllerSourceTypePhotoLibrary];

}

- (BOOL) canUserPickVideosFromPhotoLibrary{

    return [self

            cameraSupportsMedia:(__bridgeNSString *)kUTTypeMoviesourceType:UIImagePickerControllerSourceTypePhotoLibrary];

}

- (BOOL) canUserPickPhotosFromPhotoLibrary{

    return [self

            cameraSupportsMedia:(__bridgeNSString *)kUTTypeImagesourceType:UIImagePickerControllerSourceTypePhotoLibrary];

}


- (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);

        

        

        ALAssetsLibrary *assetsLibrary;

        assetsLibrary = [[ALAssetsLibraryalloc] init];

//        NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"MyVideo"

//                                                  withExtension:@"MOV"];

        NSURL *videoURL = urlOfVideo;

        if (videoURL != nil){

            [assetsLibrarywriteVideoAtPathToSavedPhotosAlbum:videoURL

                                             completionBlock:^(NSURL *assetURL,NSError *error) {

                                                 if (error == nil){

                                                     NSLog(@"no errors happened");

                                                     NSLog(@"assetURL=%@",assetURL);//注意这里的结果

                                                  }else {

                                                     NSLog(@"Error happened while saving the video.");

                                                     NSLog(@"The error is = %@", error);

                                                  }

                                              }];

        } else {

            NSLog(@"Could not find the video in the app bundle.");

        }

        

        

    }else{

        NSLog(@"mediaType is not kUTTypeMovie");

    }


    [picker dismissViewControllerAnimated:YEScompletion:nil];

}



选择图片打印:


2014-07-08 17:00:34.408 cookbook7_12[2333:907] Picker returned successfully.

2014-07-08 17:00:34.411 cookbook7_12[2333:907] {

    UIImagePickerControllerMediaType = "public.image";

    UIImagePickerControllerOriginalImage = "<UIImage: 0x1d59d2d0>";

    UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=E60EAA03-9D65-4CB1-ACD0-8EAFC15DAD4D&ext=JPG";

}

2014-07-08 17:00:34.413 cookbook7_12[2333:907] mediaType is not kUTTypeMovie




选择视频打印:


2014-07-08 17:06:03.540 cookbook7_12[2333:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-08 17:06:03.543 cookbook7_12[2333:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-08 17:06:03.550 cookbook7_12[2333:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-08 17:06:03.578 cookbook7_12[2333:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0

2014-07-08 17:06:03.782 cookbook7_12[2333:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-08 17:06:03.837 cookbook7_12[2333:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1

2014-07-08 17:06:06.915 cookbook7_12[2333:907] [MPAVController] Autoplay: Disabling autoplay for pause

2014-07-08 17:06:06.917 cookbook7_12[2333:907] [MPAVController] Autoplay: Disabling autoplay

2014-07-08 17:06:08.533 cookbook7_12[2333:907] Picker returned successfully.

2014-07-08 17:06:08.534 cookbook7_12[2333:907] {

    UIImagePickerControllerMediaType = "public.movie";

    UIImagePickerControllerMediaURL = "file://localhost/private/var/mobile/Applications/19E5F719-0326-4788-B127-E618CC0C6523/tmp//trim.SK1YV9.MOV";

    UIImagePickerControllerReferenceURL = "assets-library://asset/asset.MOV?id=86E60C71-CEFD-4F98-A7B6-0DA40E04D5FA&ext=MOV";

}

2014-07-08 17:06:08.535 cookbook7_12[2333:907] Video URL = file://localhost/private/var/mobile/Applications/19E5F719-0326-4788-B127-E618CC0C6523/tmp//trim.SK1YV9.MOV

2014-07-08 17:06:08.937 cookbook7_12[2333:907] no errors happened

2014-07-08 17:06:08.938 cookbook7_12[2333:907] assetURL=assets-library://asset/asset.MOV?id=DA4F7D01-AB86-4624-8A55-22EC7BE89FE3&ext=MOV



15.7. Retrieving Assets from the Assets Library

不通过GUI直接获取图片


-(void)testAsset

{

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

//    }

    

    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibraryalloc] init];

    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll

                                usingBlock:^(ALAssetsGroup *group,BOOL *stop) {

        [group enumerateAssetsUsingBlock:^(ALAsset *result,

                                          NSUInteger index,

                                          BOOL *stop) {

            /* Get the asset type */

            NSString *assetType = [result valueForProperty:ALAssetPropertyType];

            NSLog(@"assetType=%@",assetType);

            

            if ([assetType isEqualToString:ALAssetTypePhoto]){

                NSLog(@"This is a photo asset");

            }elseif ([assetType isEqualToString:ALAssetTypeVideo]){

                NSLog(@"This is a video asset");

            }elseif ([assetType isEqualToString:ALAssetTypeUnknown]){

                NSLog(@"This is an unknown asset");

            }

            /* Get the URLs for the asset */

            NSDictionary *assetURLs = [result valueForProperty:ALAssetPropertyURLs];

            NSUInteger assetCounter = 0;

            for (NSString *assetURLKey in assetURLs){

                assetCounter++;

                NSLog(@"Asset URL %lu = %@",(unsignedlong)assetCounter, [assetURLs valueForKey:assetURLKey]);

            }

            /* Get the asset's representation object */

            ALAssetRepresentation *assetRepresentation =[result defaultRepresentation];

            NSLog(@"Representation Size = %lld",[assetRepresentationsize]);

        }];

    }

                                   failureBlock:^(NSError *error) {

                                       NSLog(@"Failed to enumerate the asset groups.");

                                    }];

}



打印:


2014-07-08 17:30:09.317 cookbook7_12[2370:907] assetType=ALAssetTypePhoto

2014-07-08 17:30:09.321 cookbook7_12[2370:907] This is a photo asset

2014-07-08 17:30:09.327 cookbook7_12[2370:907] Asset URL 1 = assets-library://asset/asset.PNG?id=F98189FD-D544-4D7F-9F6E-E295D5AEF249&ext=PNG

2014-07-08 17:30:09.349 cookbook7_12[2370:907] Representation Size = 37267

2014-07-08 17:30:09.352 cookbook7_12[2370:907] assetType=ALAssetTypePhoto

2014-07-08 17:30:09.354 cookbook7_12[2370:907] This is a photo asset

2014-07-08 17:30:09.357 cookbook7_12[2370:907] Asset URL 1 = assets-library://asset/asset.PNG?id=A4078424-259B-4087-8E64-756E5F7D726A&ext=PNG

2014-07-08 17:30:09.363 cookbook7_12[2370:907] Representation Size = 66405


……


2014-07-08 17:30:10.897 cookbook7_12[2370:907] assetType=ALAssetTypePhoto

2014-07-08 17:30:10.899 cookbook7_12[2370:907] This is a photo asset

2014-07-08 17:30:10.902 cookbook7_12[2370:907] Asset URL 1 = assets-library://asset/asset.JPG?id=608976BA-32F6-46FF-86F6-3CE6325C0342&ext=JPG

2014-07-08 17:30:10.910 cookbook7_12[2370:907] Representation Size = 134396

2014-07-08 17:30:10.912 cookbook7_12[2370:907] assetType=ALAssetTypeVideo

2014-07-08 17:30:10.914 cookbook7_12[2370:907] This is a video asset

2014-07-08 17:30:10.917 cookbook7_12[2370:907] Asset URL 1 = assets-library://asset/asset.MOV?id=D88D0485-CC8B-45F7-88F5-76C59A1F2CB8&ext=MOV

2014-07-08 17:30:10.925 cookbook7_12[2370:907] Representation Size = 373170

2014-07-08 17:30:10.927 cookbook7_12[2370:907] assetType=ALAssetTypeVideo

2014-07-08 17:30:10.928 cookbook7_12[2370:907] This is a video asset

2014-07-08 17:30:10.932 cookbook7_12[2370:907] Asset URL 1 = assets-library://asset/asset.MOV?id=86E60C71-CEFD-4F98-A7B6-0DA40E04D5FA&ext=MOV

2014-07-08 17:30:10.939 cookbook7_12[2370:907] Representation Size = 152308

2014-07-08 17:30:10.941 cookbook7_12[2370:907] assetType=ALAssetTypeVideo

2014-07-08 17:30:10.943 cookbook7_12[2370:907] This is a video asset

2014-07-08 17:30:10.946 cookbook7_12[2370:907] Asset URL 1 = assets-library://asset/asset.MOV?id=DA4F7D01-AB86-4624-8A55-22EC7BE89FE3&ext=MOV

2014-07-08 17:30:10.954 cookbook7_12[2370:907] Representation Size = 150196

2014-07-08 17:30:10.958 cookbook7_12[2370:907] assetType=(null)

2014-07-08 17:30:10.960 cookbook7_12[2370:907] Representation Size = 0



enum {

    ALAssetsGroupLibrary        = (1 <<0),         // The Library group that includes all assets.

    ALAssetsGroupAlbum          = (1 <<1),        // All the albums synced from iTunes or created on the device.

    ALAssetsGroupEvent          = (1 <<2),         // All the events synced from iTunes.

    ALAssetsGroupFaces          = (1 <<3),         // All the faces albums synced from iTunes.

    ALAssetsGroupSavedPhotos    = (1 <<4),         // The Saved Photos album.

#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED

    ALAssetsGroupPhotoStream    = (1 <<5),         // The PhotoStream album.

#endif

    ALAssetsGroupAll            = 0xFFFFFFFF,      // The same as ORing together all the available group types,

};

typedefNSUInteger ALAssetsGroupType;





显示照片库中的第一张照片


-(void)testDisplayTheFirstPhoto

{

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

//    }

    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibraryalloc] init];

    dispatch_queue_t dispatchQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

    dispatch_async(dispatchQueue, ^(void) {

        [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAllusingBlock:^(ALAssetsGroup *group,BOOL *stop) {

            [groupenumerateAssetsUsingBlock:^(ALAsset *result,

                                              NSUInteger index,

                                              BOOL *stop) {

                __block BOOL foundThePhoto = NO;

                if (foundThePhoto){

                    *stop =YES;

                }

                /* Get the asset type */

                NSString *assetType =[result valueForProperty:ALAssetPropertyType];

                if ([assetType isEqualToString:ALAssetTypePhoto]){

                    NSLog(@"This is a photo asset");

                    foundThePhoto =YES;

                    *stop =YES;

                    /* Get the asset's representation object */

                   ALAssetRepresentation *assetRepresentation =[resultdefaultRepresentation];

                    /* We need the scale and orientation to be able to

                     construct a properly oriented and scaled UIImage

                     out of the representation object */

                   CGFloat imageScale = [assetRepresentation scale];

                   UIImageOrientation imageOrientation =(UIImageOrientation)[assetRepresentationorientation];

                   dispatch_async(dispatch_get_main_queue(), ^(void) {

                       CGImageRef imageReference =[assetRepresentationfullResolutionImage];

                        /* Construct the image now */

                       UIImage *image =[[UIImagealloc] initWithCGImage:imageReference

                                                  scale:imageScale

                                            orientation:imageOrientation];

                       if (image != nil){

                           UIImageView *imageView = [[UIImageViewalloc] initWithFrame:self.view.bounds];

                            imageView.contentMode =UIViewContentModeScaleAspectFit;

                            imageView.image = image;

                            [self.viewaddSubview:imageView];

                        }else {

                           NSLog(@"Failed to create the image.");

                        }

                    });

                }

            }];

        }

                                       failureBlock:^(NSError *error) {

                                           NSLog(@"Failed to enumerate the asset groups.");

                                        }];

    });

    

}


打印:

2014-07-08 17:55:00.192 cookbook7_12[2388:31f] This is a photo asset

2014-07-08 17:55:00.216 cookbook7_12[2388:31f] This is a photo asset

2014-07-08 17:55:00.301 cookbook7_12[2388:907] invalid attempt to access <ALAssetRepresentationPrivate: 0x1ed7e420> past the lifetime of its owning ALAssetsLibrary




对于视频文件,处理起来有些不同,因为ALAssetRepresentation没有返回视频文件对象的方法。所以,我们需要把文件读入缓存然后再保存起来

看看下面的代码,还真有些麻烦,话说 ALAssetsLibrary有提供视频拷贝的照片库的方法,为什么不提供拷贝到指定目录下的方法呢

-(void)testCopyVideoAsset{

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

//    }

    

   ALAssetsLibrary * assetsLibrary = [[ALAssetsLibraryalloc] init];

    dispatch_queue_t dispatchQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

   dispatch_async(dispatchQueue, ^(void) {

        [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll

                                    usingBlock:^(ALAssetsGroup *group,BOOL *stop) {

           __block BOOL foundTheVideo =NO;

            [groupenumerateAssetsUsingBlock:^(ALAsset *result,

                                              NSUInteger index,

                                              BOOL *stop) {

                /* Get the asset type */

               NSString *assetType = [result valueForProperty:ALAssetPropertyType];

               if ([assetType isEqualToString:ALAssetTypeVideo]){

                    NSLog(@"This is a video asset");

                    foundTheVideo =YES;

                    *stop =YES;

                    /* Get the asset's representation object */

                   ALAssetRepresentation *assetRepresentation =[resultdefaultRepresentation];

                   const NSUInteger BufferSize =1024;

                   uint8_t buffer[BufferSize];

                   NSUInteger bytesRead = 0;

                   long long currentOffset =0;

                   NSError *readingError = nil;

                    /* Construct the path where the video has to be saved */

                   NSString *videoPath =[[selfdocumentFolderPath] stringByAppendingPathComponent:@"Temp.MOV"];

                   NSFileManager *fileManager = [[NSFileManageralloc] init];

                    /* Create the file if it doesn't exist already */

                   if ([fileManager fileExistsAtPath:videoPath] ==NO){

                        [fileManagercreateFileAtPath:videoPath contents:nil attributes:nil];

                    }

                    /* We will use this file handle to write the contents

                     of the media assets to the disk */

                   NSFileHandle *fileHandle =[NSFileHandlefileHandleForWritingAtPath:videoPath];

                   do{

                        /* Read as many bytes as we can put in the buffer */

                        bytesRead =

                        [assetRepresentationgetBytes:(uint8_t *)&buffer

                                          fromOffset:currentOffset

                                              length:BufferSize

                                               error:&readingError];

                        /* If we couldn't read anything, we will

                         exit this loop */

                       if (bytesRead == 0){

                           break;

                        }

                        /* Keep the offset up to date */

                        currentOffset += bytesRead;

                        /* Put the buffer into an NSData */

                       NSData *readData = [[NSDataalloc] initWithBytes:(constvoid *)buffer

                                                                 length:bytesRead];

                        /* And write the data to file */

                        [fileHandlewriteData:readData];

                    }while (bytesRead > 0);

                    NSLog(@"Finished reading and storing the \

                          video in the documents folder");

                }

            }];

                                         

           if (foundTheVideo){

                *stop =YES;

            }

        }

                                       failureBlock:^(NSError *error) {

                                           NSLog(@"Failed to enumerate the asset groups.");

                                        }];

    });

    

}



- (NSString *) documentFolderPath{

   NSFileManager *fileManager = [[NSFileManageralloc] init];

    NSURL *url = [fileManagerURLForDirectory:NSDocumentDirectory

                                    inDomain:NSUserDomainMask

                           appropriateForURL:nil

                                      create:NO

                                       error:nil];

   return url.path;

}


打印:

2014-07-09 11:01:20.433 cookbook7_12[2465:1103] This is a video asset

2014-07-09 11:01:21.090 cookbook7_12[2465:1103] Finished reading and storing the                           video in the documents folder


  • 15.8. Editing Videos on an iOS Device 

在应用中编辑视频

-(void)testVideoEditor

{

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

//    }

    self.assetsLibrary = [[ALAssetsLibraryalloc] init];

    if ([selfisPhotoLibraryAvailable] &&[selfcanUserPickVideosFromPhotoLibrary]){

        UIImagePickerController *imagePicker =[[UIImagePickerControlleralloc] init];

        /* Set the source type to photo library */

        imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

        /* And we want our user to be able to pick movies from the library */

        imagePicker.mediaTypes =@[(__bridgeNSString *)kUTTypeMovie];

        /* Set the delegate to the current view controller */

        imagePicker.delegate =self; /* Present our image picker */

        [selfpresentViewController:imagePicker animated:YEScompletion:nil];

    }

}


- (void)videoEditorController:(UIVideoEditorController *)editor didSaveEditedVideoToPath:(NSString *)editedVideoPath{

    NSLog(@"The video editor finished saving video");

    NSLog(@"The edited video path is at = %@", editedVideoPath);

    [editor dismissViewControllerAnimated:YEScompletion:nil];

}

- (void)videoEditorController:(UIVideoEditorController *)editor didFailWithError:(NSError *)error{

    NSLog(@"Video editor error occurred = %@", error);

    [editor dismissViewControllerAnimated:YEScompletion:nil];

}

- (void)videoEditorControllerDidCancel:(UIVideoEditorController *)editor{

    NSLog(@"The video editor was cancelled");

    [editor dismissViewControllerAnimated:YEScompletion:nil];

}


- (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];

    [availableMediaTypesenumerateObjectsUsingBlock:

     ^(id obj,NSUInteger idx, BOOL *stop) {

         NSString *mediaType = (NSString *)obj;

         if ([mediaType isEqualToString:paramMediaType]){

             result =YES;

             *stop=YES;

         }

     }];

    return result;

}

- (BOOL) canUserPickVideosFromPhotoLibrary{

    return [selfcameraSupportsMedia:(__bridgeNSString *)kUTTypeMoviesourceType:UIImagePickerControllerSourceTypePhotoLibrary];

}

- (BOOL) isPhotoLibraryAvailable{

    return [UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];

}


- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    NSLog(@"Picker returned successfully.");

    NSString *mediaType = [infoobjectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){

        self.videoURLToEdit = [infoobjectForKey:UIImagePickerControllerMediaURL];

    }

    [picker dismissViewControllerAnimated:YEScompletion:^{

        if (self.videoURLToEdit !=nil){

            NSString *videoPath = [self.videoURLToEditpath];

            /* First let's make sure the video editor is able to edit the

             video at the path in our documents folder */

            if ([UIVideoEditorController canEditVideoAtPath:videoPath]){

                /* Instantiate the video editor */

                UIVideoEditorController *videoEditor =[[UIVideoEditorControlleralloc] init];

                /* We become the delegate of the video editor */

                videoEditor.delegate =self;

                /* Make sure to set the path of the video */

                videoEditor.videoPath = videoPath;

                /* And present the video editor */

                [selfpresentViewController:videoEditor

                                  animated:YES

                                completion:nil];

                self.videoURLToEdit = nil;

            } else {

                NSLog(@"Cannot edit the video at this path");

            }

        }

    }];

}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    NSLog(@"Picker was cancelled");

    self.videoURLToEdit =nil;

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}


打印:

2014-07-09 14:40:09.425 cookbook7_12[2718:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-09 14:40:09.427 cookbook7_12[2718:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-09 14:40:09.437 cookbook7_12[2718:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-09 14:40:09.456 cookbook7_12[2718:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0

2014-07-09 14:40:09.630 cookbook7_12[2718:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-09 14:40:09.677 cookbook7_12[2718:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1

2014-07-09 14:40:14.165 cookbook7_12[2718:907] [MPAVController] Autoplay: Disabling autoplay for pause

2014-07-09 14:40:14.167 cookbook7_12[2718:907] [MPAVController] Autoplay: Disabling autoplay

2014-07-09 14:40:14.382 cookbook7_12[2718:907] Picker returned successfully.

2014-07-09 14:40:15.936 cookbook7_12[2718:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-09 14:40:15.958 cookbook7_12[2718:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-09 14:40:15.980 cookbook7_12[2718:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0

2014-07-09 14:40:16.113 cookbook7_12[2718:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

2014-07-09 14:40:16.179 cookbook7_12[2718:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1

2014-07-09 14:40:25.819 cookbook7_12[2718:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 1 -> 1

2014-07-09 14:40:26.396 cookbook7_12[2718:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 1 -> 1

2014-07-09 14:40:29.851 cookbook7_12[2718:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 1 -> 1

2014-07-09 14:40:32.374 cookbook7_12[2718:907] [MPAVController] Autoplay: Enabling autoplay

2014-07-09 14:40:32.402 cookbook7_12[2718:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 1 -> 1

2014-07-09 14:40:33.141 cookbook7_12[2718:907] [MPAVController] Autoplay: Disabling autoplay for pause

2014-07-09 14:40:33.143 cookbook7_12[2718:907] [MPAVController] Autoplay: Disabling autoplay

2014-07-09 14:40:33.439 cookbook7_12[2718:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 1 -> 1

2014-07-09 14:40:38.995 cookbook7_12[2718:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 1 -> 1

2014-07-09 14:40:43.319 cookbook7_12[2718:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 1 -> 1

2014-07-09 14:40:44.087 cookbook7_12[2718:907] [MPAVController] Autoplay: _streamLikelyToKeepUp: 1 -> 1

2014-07-09 14:40:48.859 cookbook7_12[2718:907] [MPAVController] Autoplay: Disabling autoplay for pause

2014-07-09 14:40:48.861 cookbook7_12[2718:907] [MPAVController] Autoplay: Disabling autoplay

2014-07-09 14:40:48.937 cookbook7_12[2718:907] The video editor finished saving video

2014-07-09 14:40:48.940 cookbook7_12[2718:907] The edited video path is at = /private/var/mobile/Applications/19E5F719-0326-4788-B127-E618CC0C6523/tmp//trim.A5gtuQ.MOV

2014-07-09 14:40:48.944 cookbook7_12[2718:907] [MPAVController] Autoplay: Disabling autoplay for pause

2014-07-09 14:40:48.945 cookbook7_12[2718:907] [MPAVController] Autoplay: Disabling autoplay




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值