【问题标题】:IOS. Sharing image to Instagram without using a menu displayIOS。在不使用菜单显示的情况下将图像共享到 Instagram
【发布时间】:2016-07-11 09:25:16
【问题描述】:

谁能帮助并解释如何在不显示菜单的情况下分享到 Instagram?例如,应用程序 Prisma 不使用菜单显示来执行此操作。

【问题讨论】:

  • 你说的菜单是 UIActivityController 吗?
  • 我的意思是 UIDocumentInteractionController。
  • 我无法在此处访问此 instagram 文档,其中包含相关信息 instagram.com/developer/mobile-sharing/iphone-hooks/# 。您可以搜索“iPhone Instagram Hooks”了解更多信息。
  • 谢谢,但我对这个指令很熟悉,这不是我要找的。​​span>

标签: ios instagram-api


【解决方案1】:

你必须这样做:

  1. 将照片保存到照片库
  2. 获取已保存照片的资产网址
  3. 使用此 URL 通过 URL Scheme 打开 Instagram

instagram://library?AssetPath=ASSET_PATH

其中 ASSET_PATH 是第二步获取的资产 url。

Swift 3 上的代码示例:

let library = ALAssetsLibrary()
library.writeImage(toSavedPhotosAlbum: image.cgImage, metadata: nil) { (url, error) in
    if let url = url {
        DispatchQueue.main.async {
            let path = url.absoluteString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
            let instagram = URL(string: "instagram://library?AssetPath=\(path)")
            UIApplication.shared.open(instagram!)
        }
    }
}

【讨论】:

    【解决方案2】:

    我知道这是一个老问题,但我只是想为那些可能正在寻找的人提供一个更新的答案。

    Vitaly Berg 采用了正确的方法,只是使用了已弃用的代码。这是对我有用的最新代码(Swift 5):

    @IBAction func instagramButtonClicked(_ sender: Any) {
    
                //check and see if we can save photos
                let status = PHPhotoLibrary.authorizationStatus()
                if (status == PHAuthorizationStatus.authorized) {
                    // Access has been granted.
                    self.shareToInstagram(theImage!)
                }else if (status == PHAuthorizationStatus.denied) {
                    // Access has been denied.
                    Notifications.showNotification("Please Allow Access To Photos To Share", style: .danger)
                }else if (status == PHAuthorizationStatus.notDetermined) {
                    // Access has not been determined.
                    PHPhotoLibrary.requestAuthorization({ (newStatus) in
                        if (newStatus == PHAuthorizationStatus.authorized) {
                            self.shareToInstagram(theImage!)
                        }else {
                            Notifications.showNotification("Please Allow Access To Photos To Share", style: .danger)
                        }
                    })
                }else if (status == PHAuthorizationStatus.restricted) {
                    // Restricted access - normally won't happen.
                    Notifications.showNotification("Please Allow Access To Photos To Share", style: .danger)
                }
    }
    
    
    func shareToInstagram(_ theImageToShare: UIImage){
        self.saveToCameraRoll(image: theImageToShare) { (theUrl) in
            if let url = theUrl {
                DispatchQueue.main.async {
                    if let path = url.absoluteString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed){
                        let instagram = URL(string: "instagram://library?AssetPath=\(path)")
                        UIApplication.shared.open(instagram!)
                    }
                }
            }
        }
    }
    
    
    func saveToCameraRoll(image: UIImage, completion: @escaping (URL?) -> Void) {
        var placeHolder: PHObjectPlaceholder? = nil
        PHPhotoLibrary.shared().performChanges({
            let creationRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
            placeHolder = creationRequest.placeholderForCreatedAsset!
        }, completionHandler: { success, error in
            guard success, let placeholder = placeHolder else {
                completion(nil)
                return
            }
            let assets = PHAsset.fetchAssets(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)
            guard let asset = assets.firstObject else {
                completion(nil)
                return
            }
            asset.requestContentEditingInput(with: nil, completionHandler: { (editingInput, _) in
                completion(editingInput?.fullSizeImageURL)
            })
        })
    }
    

    希望这对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 2012-08-22
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 2013-04-08
      相关资源
      最近更新 更多