IOS。在不使用菜单显示的情况下将图像分享到Instagram

时间:2016-07-11 09:25:16

标签: ios instagram-api

任何人都可以帮助并解释如何在不显示菜单的情况下与Instagram共享吗?例如,应用程序Prisma在不使用菜单显示的情况下执行此操作。

2 个答案:

答案 0 :(得分:1)

你必须这样做:

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

    的Instagram://库AssetPath = ASSET_PATH

    ASSET_PATH是第二步的资产网址。

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

答案 1 :(得分:0)

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

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

希望这对某人有帮助!