将图像从 URL 保存到图库,重命名并使用 Swift 共享
Save image to library from URL, rename, and share it using Swift
我想从我自己的 iOS 应用程序将图像分享到 Instagram,我在整个项目中使用 Kingfisher 下载和缓存图像并在 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 的相关文档很少。我该怎么做?我只需要朝着正确的方向推动。
在我的导航控制器中,我将 UIBarButtonItem 设置为系统项 "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 列表是 complete/exhaustive 可用 UIActivityTypes 列表,从 code-complete 或 command-clicking 收集 UIActivity 类型并遍历生成的结构。我取消注释那些我希望 排除 的内容 - 但将它们保留下来以供将来快速参考。这将为您提供所需的弹出窗口。
听起来您已经知道如何使用 Kingfisher 并从 URL 中检索 UIImage。所以我要提供的是将图像保存到文档目录并检索它以共享的信息。
检索正确的目录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 以仅允许您需要的内容。
我想从我自己的 iOS 应用程序将图像分享到 Instagram,我在整个项目中使用 Kingfisher 下载和缓存图像并在 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 的相关文档很少。我该怎么做?我只需要朝着正确的方向推动。
在我的导航控制器中,我将 UIBarButtonItem 设置为系统项 "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 列表是 complete/exhaustive 可用 UIActivityTypes 列表,从 code-complete 或 command-clicking 收集 UIActivity 类型并遍历生成的结构。我取消注释那些我希望 排除 的内容 - 但将它们保留下来以供将来快速参考。这将为您提供所需的弹出窗口。
听起来您已经知道如何使用 Kingfisher 并从 URL 中检索 UIImage。所以我要提供的是将图像保存到文档目录并检索它以共享的信息。
检索正确的目录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 以仅允许您需要的内容。