如何将 UIImage 与 URL Shema 分享到 Instagram 应用程序?

How to share UIImage with URL Shema to Instagram app?

我有自己创建的图片,想通过 Instagram 应用分享它,我应该怎么做?

 - (IBAction)instagramShareTapped:(UIButton *)sender {
   UIImage *img = self.myImage;
   NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://**WHAT_SHOULD_I_PUT_HERE?**]];
   if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
     [[UIApplication sharedApplication] openURL:instagramURL];
   }
}

发现于 instagram documentation

Document Interaction

If your application creates photos and you'd like your users to share these photos using Instagram, you can use the Document Interaction API to open your photo in Instagram's sharing flow.

You must first save your file in PNG or JPEG (preferred) format and use the filename extension ".ig". Using the iOS Document Interaction APIs you can trigger the photo to be opened by Instagram. The Identifier for our Document Interaction UTI is com.instagram.photo, and it conforms to the public/jpeg and public/png UTIs. See the Apple documentation articles: Previewing and Opening Files and the UIDocumentInteractionController Class Reference for more information.

Alternatively, if you want to show only Instagram in the application list (instead of Instagram plus any other public/jpeg-conforming apps) you can specify the extension class igo, which is of type com.instagram.exclusivegram.

When triggered, Instagram will immediately present the user with our filter screen. The image is preloaded and sized appropriately for Instagram. For best results, Instagram prefers opening a JPEG that is 640px by 640px square. If the image is larger, it will be resized dynamically.

To include a pre-filled caption with your photo, you can set the annotation property on the document interaction request to an NSDictionary containing an NSString under the key "InstagramCaption". This feature is available on Instagram 2.1 and later.

以下是我如何使用 URL 架构将 UIImage 分享到 Instagram:

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    UIImage *image = // your image that you want share
    [library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) {

      NSString *escapedString = [assetURL.absoluteString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
      NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@", escapedString]];

      if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
        [[UIApplication sharedApplication] openURL:instagramURL];
      } else {
        NSLog(@"Instagram app not found.");
      }
    }];