Can I tweet an animated GIF from a UIActivityViewC

2019-03-29 01:16发布

问题:

I have a UIActivityViewController sharing an object that

  • conforms to the UIActivityItemSource protocol and
  • offers an animated GIF file by returning an NSURL of a .gif file and CFBridgingRelease(kUTTypeGIF) in the appropriate methods.

I see the GIF moving in the Simulator if I share one of my objects in a new Mail message. If I try tweeting the same thing, a frame of the image is attached to the tweet, but it's uploaded as a still image.

Do I need to wait for Apple to update UIActivityTypePostToTwitter in a future iOS version, or have I missed a workaround you've seen somewhere? (I realize this case isn't using anything from the list "Twitter.com, Android and iPhone [app]". This is probably a futile plea.)

Homework already done:

  • I know that the Twitter activity type can also work with an ALAsset object, and people get animated gifs into and out of Saved Photos by keeping their data wrapped up in ALAssets, but I want to be able to tweet these gifs without touching the Camera Roll, if possible.
  • I checked NSHipster.
  • I tried returning an NSData object instead of an NSURL for my local .gif file on a tip from Lyndsey about the Twitter API. No change.
  • I tried returning kUTTypeData instead of kUTTypeGIF as the dataTypeIdentifierForActivityType after seeing this in the Twitter API docs:

    The image passed should be the raw binary of the image or binary base64 encoded, no need to otherwise encode or escape the contents as long as the Content-Type is set appropriately (when in doubt: “application/octet-stream”).

    but then no image appeared in my tweet.

Thanks!

回答1:

As listed under "parameters" of the Twitter API:

image: The background image for the profile, base64-encoded. Must be a valid GIF, JPG, or PNG image of less than 800 kilobytes in size. Images with width larger than 2048 pixels will be forcibly scaled down. The image must be provided as raw multipart data, not a URL.



回答2:

Encountered the similar problem and Googled a lot but still not a perfect solution, the best I came up is here:

Use UIActivityItemProvider and extend - (id)item {} for different UIActivityType:

Twitter: The default UIActivityViewController Twitter share doesn't support it yet which it will "scale down" it as a still JPG. However somehow it works for GIF less than 100kb (tested in iOS 9) and I don't know why. Therefore, I have to use SLRequest to upload the GIF as taught in here. When the SLRequest is done and return, dismiss the UIActivityViewController. The downside of that is no preview share sheet and users cannot type their own message anymore.

- (id)item
{    
    if ([self.activityType isEqualToString:UIActivityTypePostToFacebook]) {
        // Upload to Giphy
        ...
        return [NSURL URLWithString:giphyURL];
    }
    if ([self.activityType isEqualToString:UIActivityTypePostToTwitter]) {
        // Use SLRequest to share instead
        ...
        // Dismiss the UIActivityViewController (I am using Unity)
        [UnityGetGLViewController() dismissViewControllerAnimated:NO completion: NULL];
        return nil;
    }
}

full code is in my GitHub, I am actually a iOS newb so some experts please correct me and the code if possible