Mar*_*ini 9 uiactionsheet ios uidocumentinteraction instagram swift
是否可以绕过行动共享表将照片分享给Instagram?
请注意,我知道的UIDocumentInteractionController和挂钩,事实上它工作正常.通过他们的示例代码,您可以获得一个Copy to Instagram选项(如果您使用独家UTI或可以处理JPG/PNG的大量应用程序,以及Instagram,则可以选择).
这很好,但我想知道是否有办法执行"复制到Instagram"操作,而无需UIDocumentInteractionController在iOS 9+中显示菜单.
为了记录,这是完美运行的代码的简化版本.假设你有一个有效的NSURL ......
guard let data: NSData = NSData(contentsOfURL: url),
image = UIImage(data: data) else {
return
}
let imageData = UIImageJPEGRepresentation(image, 100)
let captionString = "caption"
let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.ig")
guard let _ = imageData?.writeToFile(writePath, atomically: true) else {
return
}
let fileURL = NSURL(fileURLWithPath: writePath)
self.documentController = UIDocumentInteractionController(URL: fileURL)
self.documentController.delegate = self
self.documentController.UTI = "com.instagram.photo"
self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")
self.documentController.presentOpenInMenuFromRect(viewController.view.frame, inView: viewController.view, animated: true)
Run Code Online (Sandbox Code Playgroud)
该问题是,这将出现一个"动作片",我想避免这样做,如果可能的话,我想用instagram.ige(或者不管它是什么名字,使其独占),并跳过此ActionSheet.
那可能吗?
更新:我还没有找到解决方案,但似乎Instagram最终添加/添加扩展:"Instagram最近为其iOS应用程序添加了共享扩展功能.现在,您可以直接将第三方应用程序的照片分享到Instagram"来源:http://www.macworld.com/article/3080038/data-center-cloud/new-instagram-feature-for-ios-makes-it-easier-to-share-photos-from-other-apps. HTML
更新的 Swift 4.2 代码
import Photos
func postImageToInstagram(image: UIImage) {
UIImageWriteToSavedPhotosAlbum(
image,
self,
#selector(self.image(image:didFinishSavingWithError:contextInfo:)),
nil
)
}
@objc func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let err = error {
print(err) // TODO: handle error
return
}
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
if let lastAsset = fetchResult.firstObject {
let localIdentifier = lastAsset.localIdentifier
let u = "instagram://library?AssetPath=" + localIdentifier
let url = URL(string: u)!
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}
}
Run Code Online (Sandbox Code Playgroud)
原答案
import Photos
...
func postImageToInstagram(image: UIImage) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(SocialShare.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
if error != nil {
print(error)
}
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
if let lastAsset = fetchResult.firstObject as? PHAsset {
let localIdentifier = lastAsset.localIdentifier
let u = "instagram://library?LocalIdentifier=" + localIdentifier
let url = NSURL(string: u)!
if UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(NSURL(string: u)!)
} else {
let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2245 次 |
| 最近记录: |