使用 UIActivityViewController 共享视频时,邮件未附加视频
Video not attached for mail when sharing video using UIActivityViewController
我正在使用以下代码分享位于设备上的视频,它非常适合通过消息、Facebook 和 iCloud 分享,但不适用于邮件,我可以看到邮件选项在那里,但在邮件草稿中, 不附视频。
在代码中,videoAsset
是类型 PHAssetMediaTypeVideo
的 PHAsset
。
[[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
附加视频,它可以工作,我搜索但找不到答案,请帮助。
我最终将视频文件保存到文档目录,并使用文档目录中的文件 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
});
}
}];
}];
我正在使用以下代码分享位于设备上的视频,它非常适合通过消息、Facebook 和 iCloud 分享,但不适用于邮件,我可以看到邮件选项在那里,但在邮件草稿中, 不附视频。
在代码中,videoAsset
是类型 PHAssetMediaTypeVideo
的 PHAsset
。
[[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
附加视频,它可以工作,我搜索但找不到答案,请帮助。
我最终将视频文件保存到文档目录,并使用文档目录中的文件 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
});
}
}];
}];