【问题标题】:Sharing Image on instagram in ios在 ios 的 Instagram 上分享图片
【发布时间】:2013-06-06 06:10:00
【问题描述】:

我有一个应用程序,我在 instagram 上分享了一张图片,它工作得非常好。

如截图所示。当我按下 Instagram 上的按钮分享时,它会在 UIActivityviewcontroller 中打开。

如果我点击 Instagram 上的分享并直接将我带到本机 instagram 应用程序,是否有可能?

如果用户在他的设备中安装了其他应用程序,这些应用程序也会因为 UIActivityViewController 而显示。

我不想让 UIActivityviewcontroller 说“在 Instagram 中打开”。

提前致谢。

已编辑

我正在截取一个视​​图并在 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 ];
  }

【问题讨论】:

    标签: iphone ios objective-c instagram


    【解决方案1】:

    对于迅速

     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
        }
    }
    

    【讨论】:

    • 令人印象深刻的扩展!
    【解决方案2】:
    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 中打开”是可能的吗?当图像成功共享时,如何重定向到我的应用程序?
    • 是的,你可以在if语句中使用上述代码中的[[UIApplication sharedApplication] canOpenURL:instagramURL]这一行直接进入instagram原生应用。然后您将被重定向到本机应用程序。转到本机 instagram 应用程序后,您将无法被重定向到您的应用程序。您必须再次打开您的应用。
    • 好的,但什么是 instagramURL ?我试过 NSURL *instagramURL = [NSURL URLWithString:@"instagram://"];但没有打开本机 Instagram 应用
    • 请看我上面的代码。你可以看到。只需在 if 之后添加 [[UIApplication sharedApplication] canOpenURL:instagramURL] 行,它将直接打开 instagram 应用程序。
    【解决方案3】:

    您是否检查过图片的尺寸。由于它支持大于 612*612 的图片,请检查您发布的图片的尺寸

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多