【问题标题】:How can I access <Application_Home>/Library/Caches from a bundle?如何从包中访问 <Application_Home>/Library/Caches?
【发布时间】:2016-12-13 12:38:09
【问题描述】:

我的应用从服务器下载图像。我想将这些图像保存到 Caches 文件夹,然后使用UIImage(named:... 访问它们以进行内存缓存。 UIImage(named: "fileURL", in: NSBundle.mainBundle, compatibleWith: nil) 会找到 Caches 文件夹还是我需要创建一个不同的包?

【问题讨论】:

    标签: ios caching uiimage nsbundle


    【解决方案1】:

    您不想为缓存文件夹使用捆绑包。您应该使用NSFileManager 来获取缓存文件夹的路径。例如,在 Swift 2 中:

    let fileURL = try! NSFileManager.defaultManager()
        .URLForDirectory(.CachesDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
        .URLByAppendingPathComponent("test.jpg")
    
    let image = UIImage(contentsOfFile: fileURL.path!)
    

    或 Swift 3:

    let fileURL = try! FileManager.default
        .url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
        .appendingPathComponent("test.jpg")
    
    let image = UIImage(contentsOfFile: fileURL.path)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 2011-01-04
      • 2016-01-14
      • 1970-01-01
      • 2020-10-17
      • 2020-01-13
      相关资源
      最近更新 更多