iOS-将图像发送到Instagram-DocumentInteraction [英] iOS - Send Image to Instagram - DocumentInteraction

查看:146
本文介绍了iOS-将图像发送到Instagram-DocumentInteraction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以绕过Action Share Sheet向Instagram分享图片?

Would it be possible to share a picture to Instagram bypassing the Action Share Sheet?

请注意,我意识到 UIDocumentInteractionController钩子,实际上它可以正常工作.通过他们的示例代码,您可以得到Copy to Instagram选项(如果使用专有的UTI,则为唯一;或者可以与Instagram一起使用的可处理JPG/PNG的大量应用程序).

Please Note that I am aware of the UIDocumentInteractionController and the hooks and in fact it works fine. With their sample code you get a Copy to Instagram option (either unique if you use the exclusive UTI or a big list of apps which can handle a JPG/PNG, alongside with Instagram).

这很好用,但是我想知道是否有一种方法可以执行操作复制到Instagram",而无需在iOS 9+中显示UIDocumentInteractionController菜单.

This works fine, but I'd like to know if there's a way to execute the action "Copy to Instagram" without the need to present the UIDocumentInteractionController menu in iOS 9+.

为了便于记录,这是一个完美的代码简化版本.假设您有一个有效的NSURL ...

For the record this is a simplified version of the code that works perfectly. Assuming you have a valid NSURL…

        guard let data: NSData = NSData(contentsOfURL: url), 
                  image = UIImage(data: data) else {
            return
        }

        let imageData = UIImageJPEGRepresentation(image, 100)
        let captionString = "caption"
        let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.ig")

        guard let _ = imageData?.writeToFile(writePath, atomically: true) else {
            return
        }

        let fileURL = NSURL(fileURLWithPath: writePath)
        self.documentController = UIDocumentInteractionController(URL: fileURL)
        self.documentController.delegate = self
        self.documentController.UTI = "com.instagram.photo"
        self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")
        self.documentController.presentOpenInMenuFromRect(viewController.view.frame, inView: viewController.view, animated: true)

问题 是,这将显示一个操作表",如果可能的话,我想避免这样做,我想使用instagram.ige(或其他使它排他的名称) )并跳过此ActionSheet.

The problem is that this will present an "Action Sheet" and I want to avoid doing that if possible, I want to use instagram.ige (or whatever the name it was to make it exclusive) and skip this ActionSheet.

有可能吗?

更新:我还没有找到解决方案,但似乎Instagram终于在添加/添加了扩展程序:"Instagram最近在其iOS应用程序中添加了共享扩展程序功能.现在,您可以共享将来自第三方应用程序的照片直接发送到Instagram" 来源:

UPDATE: I haven't found a solution for this, but seems like Instagram finally is adding/added extensions: "Instagram recently added sharing extensions functionality to its iOS app. Now, you can share photos from third-party apps directly to Instagram" source: http://www.macworld.com/article/3080038/data-center-cloud/new-instagram-feature-for-ios-makes-it-easier-to-share-photos-from-other-apps.html

推荐答案

更新的Swift 4.2代码

import Photos

    func postImageToInstagram(image: UIImage) {
        UIImageWriteToSavedPhotosAlbum(
            image,
            self,
            #selector(self.image(image:didFinishSavingWithError:contextInfo:)),
            nil
        )
    }

    @objc func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
        if let err = error {
            print(err) // TODO: handle error
            return
        }

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
        if let lastAsset = fetchResult.firstObject {
            let localIdentifier = lastAsset.localIdentifier
            let u = "instagram://library?AssetPath=" + localIdentifier
            let url = URL(string: u)!
            if UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } else {
                let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .alert)
                alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                self.present(alertController, animated: true, completion: nil)
            }
        }
    }

原始答案

import Photos
...

func postImageToInstagram(image: UIImage) {
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(SocialShare.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
        if error != nil {
            print(error)
        }

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
        if let lastAsset = fetchResult.firstObject as? PHAsset {
            let localIdentifier = lastAsset.localIdentifier
            let u = "instagram://library?LocalIdentifier=" + localIdentifier
            let url = NSURL(string: u)!
            if UIApplication.sharedApplication().canOpenURL(url) {
                UIApplication.sharedApplication().openURL(NSURL(string: u)!)
            } else {
                let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .Alert)
                alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
                self.presentViewController(alertController, animated: true, completion: nil)
            }

        }
}

这篇关于iOS-将图像发送到Instagram-DocumentInteraction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆