视频文件只导出到照片应用?

时间:2014-01-24 09:00:47

标签: ios objective-c cocoa-touch avfoundation alassetslibrary

在我的应用程序中,我允许用户存储已编辑的视频。我将其保存到照片应用程序和我的文档目录中进行调试。但是,视频只保存到相册而不是我的应用程序,我不知道为什么。这就是我正在做的事情:

- (void) mergeVideos{
    //do some neat editing

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *tempPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@_stacked.mov", [[self.firstVideo.videoURL lastPathComponent] stringByDeletingPathExtension], [[self.secondVideo.videoURL lastPathComponent]stringByDeletingPathExtension]]];

    NSURL *url = [NSURL fileURLWithPath:tempPath];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
    exporter.outputURL=url;
    [exporter setVideoComposition:MainCompositionInst];
    exporter.outputFileType = AVFileTypeQuickTimeMovie;

    NSLog(@"EXPORT PATH IS %@",exporter.outputURL);

    [exporter exportAsynchronouslyWithCompletionHandler:^
     {
         dispatch_async(dispatch_get_main_queue(), ^{
             [self exportDidFinish:exporter];
         });
     }];
}

- (void)exportDidFinish:(AVAssetExportSession*)session
{
    NSURL *outputURL = session.outputURL;
    //NSLog(@"OUTPUT URL: %@",outputURL);
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputURL]) {
        [library writeVideoAtPathToSavedPhotosAlbum:outputURL
                                    completionBlock:^(NSURL *assetURL, NSError *error){
                                        dispatch_async(dispatch_get_main_queue(), ^{
                                            if (error) {
                                                NSLog(@"writeVideoToAssetsLibrary failed. Error: %@", error);
                                            }else{
                                                AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
                                                _managedObjectContext = [appDelegate managedObjectContext];
                                                Video *video = [NSEntityDescription insertNewObjectForEntityForName:@"Video" inManagedObjectContext:_managedObjectContext];

                                                [video setVideoURL:[NSString stringWithFormat:@"%@",outputURL]];
                                                NSError *error = nil;
                                                if (![_managedObjectContext save:&error]) {
                                                }
                                                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ALERT" message:@"SUCCESS" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                                                [alert show];
                                                NSLog(@"Successfully written!");
                                            }

                                        });

                                    }];
    }
}

0 个答案:

没有答案