[英]IOS. Sharing image to Instagram without using a menu display
任何人都可以帮助和解释如何在不显示菜单的情况下分享到 Instagram? 例如,应用程序 Prisma 无需使用菜单显示即可完成此操作。
你必须这样做:
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)
})
})
}
希望这可以帮助某人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.