Sharing Image on instagram in ios

不想你离开。 提交于 2019-11-27 06:26:11

问题


I am having an app in which I am sharing an image on instagram and it works perfectly fine.

As shown in the screenshot. When I press the button share on instagram it opens up in UIActivityviewcontroller.

Is it possible that If I click on Share on Instagram and and it takes me directly to the native instagram app?

If the user has other apps installed in his device that apps also shows because of the UIActivityViewController.

I don't want to show UIActivityviewcontroller saying "Open In Instagram".

Thanks in advance.

Edited

I am taking a screenshot of a view and sharing that screenshot on Instagram.

When I click on a button share it opens up as shown in the screenshot which I don't want.

I am using the below code.

NSURL *instagramURL = [NSURL URLWithString:@"instagram://"];

                if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
                {
                    CGRect rect = CGRectMake(0 ,0 , 0, 0);
                    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
                    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

                    UIGraphicsEndImageContext();
                    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                    NSString* documentsDirectory = [paths objectAtIndex:0];

                    imagename=[NSString stringWithFormat:@"ff.ig"];
                    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imagename];
                    ////NSLog(@"%@",fullPathToFile);

                    igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", fullPathToFile]];

                    self.dic.UTI = @"com.instagram.photo";
                    self.dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
                    self.dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];


                        self.dic.annotation = [NSDictionary dictionaryWithObject:@"Image" forKey:@"InstagramCaption"];

     [self.dic presentOpenInMenuFromRect: rect    inView:self.view animated: YES ];
  }

回答1:


for swift

 var instagramURL = NSURL(string: "instagram://app")!
        if UIApplication.sharedApplication().canOpenURL(instagramURL) {
            var documentDirectory = NSHomeDirectory().stringByAppendingPathComponent("Documents")
            var saveImagePath = documentDirectory.stringByAppendingPathComponent("Image.igo")
            var imageData = UIImagePNGRepresentation(self.bestScoreShareView.takeSnapshot(W: 640, H: 640))
            imageData.writeToFile(saveImagePath, atomically: true)
            var imageURL = NSURL.fileURLWithPath(saveImagePath)!
           docController  = UIDocumentInteractionController()
            docController.delegate = self
            docController.UTI = "com.instagram.exclusivegram"
            docController.URL = imageURL
            docController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)

        } else {
            println("instagram not found")
        }

extension for take shapshot from uiview

extension UIView {
    func takeSnapshot(#W: CGFloat, H: CGFloat) -> UIImage {
        var cropRect = CGRectMake(0, 0, 600, 600)
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
        drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        image.imageWithNewSize(CGSizeMake(W, H))
        return image
    }
}

extension for set image size

extension UIImage {
     func imageWithNewSize(newSize:CGSize) ->UIImage {
        UIGraphicsBeginImageContext(newSize)
        self.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))

        let newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
        return newImage
    }
}



回答2:


self.documentInteractionController = [self setupControllerWithURL:imgurl usingDelegate:self];

self.documentInteractionController=[UIDocumentInteractionController  interactionControllerWithURL:imgurl];

self.documentInteractionController.UTI = @"com.instagram.exclusivegram";

Use this code in same sequence . Here documentInteractionController is object of UIDocumentInteractionController.just for your knowledge. you will get instagram only in "open in" window.

Save your image with igo extension instead of ig.

By this you will only get instagram option in your "Open in" Menu.

Or if you want to open directly instagram when you press the button you can pass the direct url of the app and it will lead you to the native instagram app.

Hope it helps you.




回答3:


Have you checked the size of your image.As it supports image bigger than 612*612 do check the size of your image you are posting



来源:https://stackoverflow.com/questions/16954810/sharing-image-on-instagram-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!