首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将使用Kingfisher下载的图像保存到文档目录

将使用Kingfisher下载的图像保存到文档目录
EN

Stack Overflow用户
提问于 2017-08-29 21:04:21
回答 3查看 1.9K关注 0票数 1

我正在使用翠鸟从一组url下载图片。

我需要将下载或缓存的图像(实际大小的图像)存储在我的文档目录中。

有可能做到这一点吗?

EN

回答 3

Stack Overflow用户

发布于 2017-08-29 23:34:45

使用以下代码将图像加载到图像视图中。完成处理程序将帮助完成图像下载后的任何任务。

代码语言:javascript
复制
loadImage(fromUrl: URL(string: imageUrl)!, imageView: imgView, fileName: image.png)

func loadImage(fromUrl: URL, imageView: UIImageView, fileName: String) {
imageView.kf.indicatorType = .activity
imageView.kf.setImage(with:fromUrl, placeholder: nil, options: [.transition(.none)], progressBlock: nil, completionHandler: {(result) in
    let documentsDirectoryURL = try! FileManager().url(for:
        .documentDirectory, in: .userDomainMask, appropriateFor: nil, create:
        true)

    let fileURL =
        documentsDirectoryURL.appendingPathComponent(fileName)


    if !FileManager.default.fileExists(atPath: fileURL.path) {
        do {
            try UIImagePNGRepresentation(imageView.image!)!.write(to: fileURL)
            print("Image Added Successfully")
        } catch {
            print(error)
        }
    } else {
        print("Image Not Added")
    }
})
}
票数 1
EN

Stack Overflow用户

发布于 2021-09-26 09:29:30

翠鸟现已支持此操作,您可以这样使用:

代码语言:javascript
复制
    let downLoader = ImageDownloader.default
    let imageURL = URL(string: "http://xxxx.png")!
        
    downLoader.downloadImage(with: imageURL, completionHandler:  { result in
        switch result {
            case .success(let value):
                ImageCache.default.storeToDisk(value.originalData, forKey: "your cache key")
            case .failure(let error):
                print(error)
            }
    })
票数 1
EN

Stack Overflow用户

发布于 2017-08-29 21:23:17

试试这个:-

代码语言:javascript
复制
let documentsDirectoryURL = try! FileManager().url(for: 
.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: 
true)
// create a name for your image
let fileURL = 
documentsDirectoryURL.appendingPathComponent("imgName.png")


  if !FileManager.default.fileExists(atPath: fileURL.path) {
   do {
    try UIImagePNGRepresentation(cahedimage!)!.write(to: fileURL)
        print("Image Added Successfully")
    } catch {
        print(error)
    }
   } else {
    print("Image Not Added")
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45939959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档