Search code examples
iosobjective-cuiimageinstagraminstagram-api

How to share UIImage with URL Shema to Instagram app?


I have image I created and I want to share it trough instagram app, how should I do that?

 - (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];
   }
}

Solution

  • Here is how I could share an UIImage to Instagram using URL Schema:

        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.");
          }
        }];