我想从我自己的iOS应用程序将图像共享到Instagram,我在整个项目中都使用翠鸟来下载和缓存图像,并在UIImageViews中显示它们,但这次我想做一些不同的事情。
基本上,我从API得到了一个带有图像url的响应,我想
- 使用URL将图像下载到库
对于使用的objective-C,有很多问题
UIImageWriteToSavedPhotosAlbum
但我用的是Swift。
- 使用.igo扩展名重命名(instagram独占)
不确定如何进行,取决于数字1。
然后我可以分享它,做一些类似的事情
let image = UIImage(named: "downloadedImage") let objectsToShare: [AnyObject] = [ image! ] let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) activityViewController.popoverPresentationController?.sourceView = self.view activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook ] self.presentViewController(activityViewController, animated: true, completion: nil)
或者我可以使用instagram挂钩:
instagram://library?LocalIdentifier=(localID)
专门针对Swift的相关文档很少。我该怎么办?我只需要朝着正确的方向努力。
听起来你已经知道如何使用翠鸟并从URL中检索UIImage了。因此,我要提供的是将图像保存到documents目录并检索以共享的信息。
检索正确的目录URL
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
将图像保存到该目录
func saveImage (image: UIImage, filename: String ){
print("Saving image with name (filename)")
if let data = UIImagePNGRepresentation(image) {
let fullURL = getDocumentsDirectory().appendingPathComponent(filename)
try? data.write(to: fullURL)
}
}
从目录检索图像
func loadImageFromName(name: String) -> UIImage? {
print("Loading image with name (name)")
let path = getDocumentsDirectory().appendingPathComponent(name).path
let image = UIImage(contentsOfFile: path)
if image == nil {
print("missing image at: (path)")
}
return image
}
共享图像
func share(shareText shareText:String?,shareImage:UIImage?){
var objectsToShare = [AnyObject]()
if let shareTextObj = shareText{
objectsToShare.append(shareTextObj)
}
if let shareImageObj = shareImage{
objectsToShare.append(shareImageObj)
}
if shareText != nil || shareImage != nil{
let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
present(activityViewController, animated: true, completion: nil)
}else{
print("There is nothing to share")
}
}
如何使用
let img: UIImage = UIImage() // Replace this with your image from your URL
saveImage(image: img, filename: "newImage.igo") //This is where you change your extension name
let newImage: UIImage = loadImageFromName(name: "newImage.igo") //Retrieve your image with the correct extension
share(shareText: "Image going to Instagram", shareImage: newImage) //Present Activity VC.
正如DFD所指出的,你可以从共享VC中排除某些项目,只允许你需要的东西。
在我的导航控制器中,我有一个UIBarButtonItem设置为System项"Action"。然后我创建了以下IBAction代码:
@IBAction func shareImage(_ sender: UIBarButtonItem) {
let context = CIContext()
let final = context.createCGImage(imgFinished, from: imgFinished.extent)
let shareImage = UIImage(cgImage: final!)
let vc = UIActivityViewController(activityItems: [shareImage], applicationActivities: [])
vc.excludedActivityTypes = [
UIActivityType.airDrop,
UIActivityType.assignToContact,
UIActivityType.addToReadingList,
//UIActivityType.copyToPasteboard,
//UIActivityType.mail,
//UIActivityType.message,
//UIActivityType.openInIBooks,
//UIActivityType.postToFacebook,
//UIActivityType.postToFlickr,
UIActivityType.postToTencentWeibo,
//UIActivityType.postToTwitter,
UIActivityType.postToVimeo,
UIActivityType.postToWeibo,
UIActivityType.print,
//UIActivityType.saveToCameraRoll
]
present(vc,
animated: true,
completion: nil)
vc.popoverPresentationController?.sourceView = self.view
vc.completionWithItemsHandler = {(activity, success, items, error) in
}
}
excludedActivityTypes列表是一个完整/详尽的可用UIActivityTypes列表,这些列表是从代码完成或命令单击UIActivity类型并遍历生成的结构中收集的。我取消注释那些我希望排除的内容,但将它们保留在中,以便将来快速参考。这会给你想要的弹出窗口。