gpt4 book ai didi

ios - 从 URL 将图像保存到库,重命名并使用 Swift 共享

转载 作者:行者123 更新时间:2023-11-28 15:56:37 24 4
gpt4 key购买 nike

我想从我自己的 iOS 应用程序将图像分享到 Instagram,我在整个项目中使用 Kingfisher 下载和缓存图像并在 UIImageViews 中显示它们,但这次我想做一些不同的事情。

基本上,我从带有图像 url 的 API 获得响应,我想

  1. 使用 URL 将图像下载到库中

关于使用 objective-C 有一堆问题

 UIImageWriteToSavedPhotosAlbum 

但我使用的是 Swift。

  1. 使用 .igo 扩展重命名(instagram 独有)

不确定如何解决这个问题,这取决于第 1 条。

  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 Hook :

 instagram://library?LocalIdentifier=\(localID)

专门针对 Swift 的文档很少。我该怎么做?我只需要朝着正确的方向插入。

最佳答案

听起来您已经知道如何使用 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 - 从 URL 将图像保存到库,重命名并使用 Swift 共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41641771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com