Using UIActivityViewController to share a video

2019-04-11 23:23发布

问题:

I am trying to use the default apple share screen to share a video to things like twitter and facebook. I have got this to work with an image but I do not know how change this to work with a video. Thanks in advance!

Here is the code I am using to share the photo:

(IBAction)shareButtonPressed:(id)sender {

    UIImage *imagetoshare = [UIImage imageNamed:@"First_Time_Travel"];
    NSArray *activityItems = @[imagetoshare];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint];
    [self presentViewController:activityVC animated:TRUE completion:nil];
}

回答1:

You need a file URL to the video file or and ALAsset representing the video. Use either of those as the object in your activityItems array.



回答2:

For swift 3.0 OR up

let url = URL(fileURLWithPath: "URL String Here")
let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = controller.view // so that iPads won't crash
controller.present(activityViewController, animated: true, completion: nil)


回答3:

If you are using an avasset, you can get the url using (AVUrlAsset*)AVAsset.url