gpt4 book ai didi

ios - AWSMobileHub 使用 Swift 在 IOS 中缓存来自 S3 的图像

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:59 25 4
gpt4 key购买 nike

我正在尝试从 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)
}

完成后出现错误:domain: "com.amazonaws.AWSContentManager.ErrorDomain"- code: 1

我的代码有什么问题?

最佳答案

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

 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)
}

关于ios - AWSMobileHub 使用 Swift 在 IOS 中缓存来自 S3 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38969840/

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