Ios 网间网操作系统。在不使用菜单显示的情况下将图像共享到Instagram

Ios 网间网操作系统。在不使用菜单显示的情况下将图像共享到Instagram,ios,instagram-api,Ios,Instagram Api,有人可以帮助并解释如何在不显示菜单的情况下共享Instagram吗?例如,应用程序Prisma在不使用菜单显示的情况下执行此操作。必须执行以下操作: 将照片保存到照片库 获取已保存照片的资源url 使用此URL通过URL方案打开Instagram instagram://library?AssetPath=ASSET_PATH 其中,ASSET_PATH是在第二步中获取的资产url Swift 3上的代码示例: let library = ALAssetsLibrary() library.wr

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

必须执行以下操作:

  • 将照片保存到照片库
  • 获取已保存照片的资源url
  • 使用此URL通过URL方案打开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!)
            }
        }
    }
    
    您必须这样做:

  • 将照片保存到照片库
  • 获取已保存照片的资源url
  • 使用此URL通过URL方案打开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!)
            }
        }
    }
    

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

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

    希望这对别人有帮助

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

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

    希望这对别人有帮助

    菜单是指UIActivityController吗?我是指UIDocumentInteractionController。我无法在此处访问此instagram文档,其中包含相关信息。你可以搜索“iPhone Instagram Hooks”以获取更多信息。谢谢,但我熟悉此说明,这不是我要查找的。菜单中的“UIActivityController”是指UIDocumentInteractionController吗?我指的是UIDocumentInteractionController。我无法在此处访问此Instagram文档,其中包含相关信息。您可以搜索“iPhone Instagram Hooks”以获取更多信息。谢谢,但我熟悉此说明,这不是我想要的。