IActivityViewController导出PHAsset

IActivityViewController导出PHAsset

本文介绍了如何通过iOS中的UIActivityViewController导出PHAsset视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在iOS中通过 UIActivityViewController 导出 PHAsset 视频。
当PHAsset是图像时它工作正常。问题是当 PHAsset 是视频时。
我有以下代码导出 PHAsset 视频,但没有附加任何内容。

I tried to export PHAsset Video by UIActivityViewController in iOS.It worked fine when PHAsset was image. The problem was when PHAsset was video.I had the following code to export PHAsset video, but nothing was attached.

    var asset = getCurrentAsset() as PHAsset?

    if asset == nil {
        return
    }

    if asset?.mediaType == PHAssetMediaType.Video {

        PHImageManager.defaultManager().requestAVAssetForVideo(asset, options: nil) { (avAsset:AVAsset!, audioMix:AVAudioMix!, info:[NSObject : AnyObject]!) -> Void in
            var videoURL = avAsset as AVURLAsset
            var activityVC = UIActivityViewController(activityItems: [videoURL], applicationActivities: nil)
            activityVC.completionHandler = {(str: String!, value: Bool) -> Void in
            }
            self.presentViewController(activityVC, animated: true, completion: nil)
        }

    } 

请帮我解决如何导出 PHAsset 视频。

Please help me how to export PHAsset video.

推荐答案

我浪费了很多时间来做这件事。

I have wasted a lot of time to do that.

最后我通过将视频保存到文档目录,并使用文档目录中的文件URL。

Finally I resolved it by saving the video to document directory, and using the file url from document directory.

typeof(self) __weak weakSelf = self;

[[PHImageManager defaultManager] requestExportSessionForVideo: self.videosAssets[index] options:nil exportPreset:AVAssetExportPresetPassthrough resultHandler:^(AVAssetExportSession *exportSession, NSDictionary *info) {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* videoPath = [documentsDirectory stringByAppendingPathComponent:@"tempSelvi.MOV"];
    NSFileManager *manager = [NSFileManager defaultManager];

    NSError *error;
    if ([manager fileExistsAtPath:videoPath]) {
        BOOL success = [manager removeItemAtPath:videoPath error:&error];
        if (success) {
            NSLog(@"Already exist. Removed!");
        }
    }

    NSURL *outputURL = [NSURL fileURLWithPath:videoPath];
    NSLog(@"Final path %@",outputURL);
    exportSession.outputFileType=AVFileTypeQuickTimeMovie;
    exportSession.outputURL=outputURL;

    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        if (exportSession.status == AVAssetExportSessionStatusFailed) {
            NSLog(@"failed");
        } else if(exportSession.status == AVAssetExportSessionStatusCompleted){
            NSLog(@"completed!");
            dispatch_async(dispatch_get_main_queue(), ^(void) {
                NSArray *activityItems = [NSArray arrayWithObjects:outputURL, nil];

                UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
                activityViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
                    NSError *error;
                    if ([manager fileExistsAtPath:videoPath]) {
                        BOOL success = [manager removeItemAtPath:videoPath error:&error];
                        if (success) {
                            NSLog(@"Successfully removed temp video!");
                        }
                    }
                    [weakSelf dismissViewControllerAnimated:YES completion:nil];
                };
                [weakSelf presentViewController:activityViewController animated:YES completion:nil];
            });
        }
    }];
}];

这篇关于如何通过iOS中的UIActivityViewController导出PHAsset视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 06:39