简体   繁体   English

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

[英]IOS. Sharing image to Instagram without using a menu display

Can anyone help and explain how to do sharing to Instagram without displaying the menu?任何人都可以帮助和解释如何在不显示菜单的情况下分享到 Instagram? For example, the application Prisma does it without using a menu display.例如,应用程序 Prisma 无需使用菜单显示即可完成此操作。

You must do this:你必须这样做:

  1. Save your photo to photo library将您的照片保存到照片库
  2. Take asset url of saved photo获取已保存照片的资产网址
  3. Open Instagram through URL Scheme with this URL使用此 URL 通过 URL Scheme 打开 Instagram

instagram://library?AssetPath=ASSET_PATH instagram://library?AssetPath=ASSET_PATH

Where ASSET_PATH is asset url taken on the second step.其中 ASSET_PATH 是在第二步中获取的资产 url。

Code example on Swift 3 : 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!)
        }
    }
}

I know this is an old questions but I just wanted to provide an updated answer for those who may be looking.我知道这是一个老问题,但我只是想为那些可能正在寻找的人提供更新的答案。

Vitaly Berg has the right approach just with deprecated code. Vitaly Berg 对弃用的代码有正确的方法。 Here is up to date code (Swift 5) That worked for me:这是对我有用的最新代码(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)
        })
    })
}

Hope this helps someone!希望这可以帮助某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM