使用UIActivityViewController共享视​​频时,不附加邮件的视频 [英] Video not attached for mail when sharing video using UIActivityViewController

查看:327
本文介绍了使用UIActivityViewController共享视​​频时,不附加邮件的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码来分享一个位于设备上的视频,它通过邮件,Facebook和iCloud共享,不仅仅是邮件,我可以看到邮件选项是在那里,但是在邮件草稿中,视频没有附加。

I am using the below code to share a video located on device, it works great for sharing via message, facebook and iCloud, only not for mail, I can see the mail option is there, but in the mail draft, the video is not attached.

在代码中, videoAsset 是一个 PHAsset 键入 PHAssetMediaTypeVideo

In the code, videoAsset is a PHAsset of type 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 添加视频,它的作品,我搜索,但找不到答案,请帮助。

If I attach a video using UIImagePickerController, it works, I searched but couldn't find an answer, please help.

推荐答案

我最终将视频文件保存到文档目录,并使用文件目录中的文件url,该视频被附加通过邮件共享。

I ended up saving the video file to document directory, and using the file url from document directory, the video is attached for sharing via mail.

[[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 方法导出视频到文件目录,这也是有效的。

I also used requestExportSessionForVideo method to export a video to document directory, which also worked.

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

                });


        }
    }];
}];

这篇关于使用UIActivityViewController共享视​​频时,不附加邮件的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆