使用UIActivityViewController共享视​​频时,视频未附加邮件

时间:2015-01-29 07:17:19

标签: ios objective-c email video uiactivityviewcontroller

我使用下面的代码分享位于设备上的视频,它非常适合通过消息,facebook和iCloud共享,但不适用于邮件,我可以看到邮件选项在那里,但在邮件草稿中,视频未附加。

在代码中,videoAssetPHAsset类型的PHAssetMediaTypeVideo

[[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
    AVURLAsset* urlAsset = (AVURLAsset*)asset;
    fileUrl = urlAsset.URL;
    NSLog(@"fileUrl is %@",fileUrl);

    NSArray *activityItems = [NSArray arrayWithObjects:fileUrl, nil];

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityViewController animated:YES completion:nil];
}];

如果我使用UIImagePickerController附加视频,它可以播放,我搜索但无法找到答案,请提供帮助。

1 个答案:

答案 0 :(得分:12)

我最终将视频文件保存到文档目录,并使用文档目录中的文件URL附加视频,以便通过邮件进行共享。

[[PHImageManager defaultManager] requestImageDataForAsset:videoAsset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
    NSLog(@"info is %@", info);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* videoPath = [documentsDirectory stringByAppendingPathComponent:@"IMG_2185.MOV"];

    NSError *error;
    [[NSFileManager defaultManager] removeItemAtPath:videoPath error:&error];
    BOOL success = [imageData writeToFile:videoPath atomically:YES];
    if (success) {
        NSArray *activityItems = [NSArray arrayWithObjects:[NSURL fileURLWithPath:videoPath], nil];

        UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
        [self presentViewController:activityViewController animated:YES completion:nil];
    }
}];

我还使用requestExportSessionForVideo方法将视频导出到文档目录,这也很有效。

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

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

    NSError *error;
    if ([manager fileExistsAtPath:videoPath]) {
        BOOL success = [manager removeItemAtPath:videoPath error:&error];
        if (success) {
            NSLog(@"I successfully removed it!");
        }
    }

    NSURL *outputURL = [NSURL fileURLWithPath:videoPath];
    NSLog(@"this is the 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];
                    [self presentViewController:activityViewController animated:YES completion:nil];
                    //Main

                });


        }
    }];
}];