2

我正在尝试从 S3 下载图像以缓存它。这没有缓存是有效的:

let manager = AWSUserFileManager.defaultUserFileManager()
let content = manager.contentWithKey("public%2F" + userID + "_profile.jpg")
content.getRemoteFileURLWithCompletionHandler({[weak self](url: NSURL?, error: NSError?) -> Void in
guard let url = url else {
         print("Error getting URL for file. \(error)")
         return
         }
         let imageData = NSData(contentsOfURL: url)
         imageView.image = UIImage(data: imageData!)
         })

这与缓存不起作用:

      let content = manager.contentWithKey("public%2F" + userID + "_profile.jpg")
    if !content.cached {
        print("Not Cached")
        imageView.image = UIImage(named: "UserIcon")
        content.downloadWithDownloadType(AWSContentDownloadType.IfNewerExists, pinOnCompletion: false, progressBlock: nil, completionHandler: { (content: AWSContent?, data: NSData?, error: NSError?) -> Void in
                print("Cached")
                imageView.image = UIImage(data: data!)
        })
    } else {
        print("Cached2")
        imageView.image = UIImage(data: content.cachedData)
    }

完成后出现错误:域:“com.amazonaws.AWSContentManager.ErrorDomain” - 代码:1

我的代码有什么问题?

4

2 回答 2

0

我发现了。有必要使用进度!

 if !content.cached {
        print("Not Cached")
        imageView.image = UIImage(named: "UserIcon")
        content.downloadWithDownloadType(AWSContentDownloadType.IfNewerExists, pinOnCompletion: false, progressBlock: {(content: AWSContent?, progress: NSProgress?) -> Void in
             return
            }, completionHandler: { (content: AWSContent?, data: NSData?, error: NSError?) -> Void in
            if let error = error {
               print("Failed to download a content from a server.)")
                print("Error " + error.debugDescription)
                return
                }
            print("Cached")
            imageView.image = UIImage(data: data!)

        })
    } else {
        print("Cached2")
        imageView.image = UIImage(data: content.cachedData)
    }
于 2016-08-17T16:06:56.657 回答
-1

这可能是因为它正在下载相同的内容,而另一个仍在下载。至少这是我的问题。

于 2016-10-09T15:32:00.663 回答