如何将多个视频保存到应用程序文档目录中?

时间:2016-03-10 06:10:55

标签: ios objective-c video nsdocumentdirectory phasset

我正在使用GMImagePicker来获取相机胶卷的资源。 选择单个或多个视频后,我在arrayArray中获得了PHAsset。 assetArray此格式中的数据

 "<PHAsset: 0x7fe0e8f21130> 40D4733D-4C7B-443D-8093-C28E39ACA45E/L0/001 mediaType=2/0, sourceType=1, (720x480), creationDate=2016-02-14 09:50:58 +0000, location=0, hidden=0, favorite=0 " 

如何将此视频存储到我的应用程序文档目录中? 我用来存储视频的代码是

for (PHAsset *asset in assetArray) {

        if (asset.mediaType==PHAssetMediaTypeVideo) {
            NSLog(@"Video found");
        }
        PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
        options.version = PHVideoRequestOptionsVersionOriginal;

        [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {

            if ([asset isKindOfClass:[AVURLAsset class]]) {
                NSURL *URL = [(AVURLAsset *)asset URL];

               NSMutableData *responseData=[[NSMutableData alloc] init];
               responseData = [NSMutableData dataWithContentsOfURL:URL];
                if (responseData.length>0) {
                        NSString * lastPath = [URL.absoluteString lastPathComponent];//videoname.mp4
                        NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)objectAtIndex:0];
                        NSString *filePath = [NSString stringWithFormat:@"%@/%@",docDirPath,lastPath];

                       [responseData writeToFile:filePath atomically:YES];
            }
        }];

       }

我还检查了保存视频的文件路径。视频可用,但我收到此错误。

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

根据我使用NSMutableData而不是NSData,它接受Nullable值

self.responseData = [NSMutableData dataWithContentsOfURL:location];
if (self.responseData.length>0) {
        dispatch_async(dispatch_get_main_queue(), ^{
        NSString * lastPath = [url.absoluteString lastPathComponent];//videoname.mp4
        NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/%@",docDirPath,lastPath];
            [self.responseData writeToFile:filePath atomically:YES];
}
    });

希望这对你有所帮助。

答案 1 :(得分:0)

用于存储图像和视频

 for (PHAsset *asset in assetArray) {

        if (asset.mediaType==PHAssetMediaTypeVideo) {
            NSLog(@"Video found");

            PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
            options.version = PHVideoRequestOptionsVersionOriginal;

            [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {

                if ([asset isKindOfClass:[AVURLAsset class]]) {
                    NSURL *URL = [(AVURLAsset *)asset URL];
                   [Albumvideos addObject:URL];
                    NSData *videoData = [NSData dataWithContentsOfURL:URL];
                    if (videoData.length>0) {
                        NSString *lastPath = [URL.absoluteString lastPathComponent];
                        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                        NSString *documentsDirectory = [paths objectAtIndex:0];
                        NSFileManager *fileManager = [NSFileManager defaultManager];
                        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@",TimeStamp,lastPath]];
                        [fileManager createFileAtPath:fullPath contents:videoData attributes:nil];   
                    }
                }
            }]; 
        }
else{

    self.requestOptions = [[PHImageRequestOptions alloc] init];
    self.requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
    self.requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

    // this one is key
    self.requestOptions.synchronous = true;

    //  self.assets = [NSMutableArray arrayWithArray:assets];
    PHImageManager *manager = [PHImageManager defaultManager];

    // assets contains PHAsset objects.
    __block UIImage *ima;


        // Do something with the asset

        [manager requestImageForAsset:asset
                           targetSize:PHImageManagerMaximumSize
                          contentMode:PHImageContentModeDefault
                              options:self.requestOptions
                        resultHandler:^void(UIImage *image, NSDictionary *info) {
                            ima = image;

                            [Albumimages addObject:ima];

                        }];

}
}

Albumvideos是一个可变数组,其中包含视频网址,而Albumimages包含所选图像。