在iOS上分享图片到Instagram

3

我有一个应用程序,在其中分享图片到Instagram,它能够正常工作。如截图所示,当我点击分享到Instagram按钮时,它会在UIActivityViewController中打开。

是否可能,如果我点击分享到Instagram,直接进入本机的Instagram应用程序?

如果用户在设备上安装了其他应用程序,则由于UIActivityViewController,这些应用程序也会显示。

我不想显示UIActivityViewController,并说“在Instagram中打开”。

提前致谢。

enter image description here

编辑

我正在截取视图并分享该截图到Instagram。

当我点击分享按钮时,它会打开如截图所示的界面,但我不想要这样。

我正在使用以下代码。

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 ];
  }
3个回答

3

适用于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")
        }

从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 UIImage {
     func imageWithNewSize(newSize:CGSize) ->UIImage {
        UIGraphicsBeginImageContext(newSize)
        self.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))

        let newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
        return newImage
    }
}

令人印象深刻的扩展! - Fattie

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

self.documentInteractionController=[UIDocumentInteractionController  interactionControllerWithURL:imgurl];

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

请按照相同的顺序使用此代码。这里的documentInteractionController是UIDocumentInteractionController对象,仅供您了解。您将只在“打开方式”窗口中获得Instagram。
请使用igo扩展名保存您的图像,而不是ig。
通过这种方式,您只能在“打开方式”菜单中获得Instagram选项。
或者,如果您想在按下按钮时直接打开Instagram,可以传递应用程序的直接URL,它将引导您进入本机Instagram应用程序。
希望对您有所帮助。

感谢回复,我的图片已经成功分享了,但是我想分享图片时不要出现“在Instagram中打开”的选项,这有可能吗? 当图片成功分享后,如何将用户重定向到我的应用程序? - kirti Chavda
是的,您可以在上面的代码中使用**[[UIApplication sharedApplication] canOpenURL:instagramURL]**行直接进入本机Instagram应用程序。然后您将被重定向到本机应用程序。一旦您进入本机Instagram应用程序,就无法重定向回您的应用程序。您必须再次打开您的应用程序。 - Manthan
1
好的,但是 InstagramURL 是什么?我尝试了 NSURL *instagramURL = [NSURL URLWithString:@"instagram://"]; 但没有打开本地的 Instagram 应用程序。 - kirti Chavda
只需查看我的上面的代码,您就可以看到。只需在if之后添加一行*[[UIApplication sharedApplication] canOpenURL:instagramURL]*,它将直接打开Instagram应用程序。 - Manthan

0

你有检查过你的图片大小吗?因为它支持大于612*612的图片,请检查你要发布的图片大小。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接