How to save multiple videos into application document directory?

Viewed 822

I am using GMImagePicker to fetch assets of camera roll. After selecting single or multiple videos i got PHAsset in arrayArray. assetArray data in this formate

 "<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 " 

How can i store this video into my application document directory? The code i did to store video is

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

       }

I also checked filepath where video is saved. Video is available but i get this error.

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

Please help me to solve this.

2 Answers
Related