保存/重新加载图像,并删除旧图像
我正在从 Web 服务器加载图像文件,然后将其保存到 nsuserdefaults。保存代码大致如下:
NSData *imageData = UIImagePNGRepresentation(theImage); // theImage is a UIImage
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:imageData forKey:string]; // string is a 3-dig number which identifies image
现在,显然我已经走错了路,因为不建议将图像用于 nsuserdefaults..
但我想要做的是将这些图像保存在某个地方,这样就可以访问它们而无需重新下载他们。另外,我想删除旧图像(比如说一天前的图像,或者只保留最后 10 个下载的图像)?有什么好的技术吗?
I'm loading an image file from a web server, and then saving it to nsuserdefaults. the save code is roughly:
NSData *imageData = UIImagePNGRepresentation(theImage); // theImage is a UIImage
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:imageData forKey:string]; // string is a 3-dig number which identifies image
Now, apparently I'm on the wrong foot already, since images are not recommended for nsuserdefaults..
But what I want to do is save these images somewhere, so they can be accessed without having to re-download them. Additionally, I would like to delete old images (let's say a day old, or keep only the last 10 downloaded images)? Is there a good technique for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的目的,您可以在多个位置保存下载的图像:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]
[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0]
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
NSTemporaryDirectory()
如果您想要任何保留规则,例如“仅保留 10”或“一天后删除”,您必须自己在代码中实现它。您可以依赖文件修改日期(来自 NSFileManager 的
attributesOfItemAtPath:error:
),或者将文件名到日期的映射保留在 NSUserDefaults 或 Core Data 中,或者只是命名文件以包含日期。Depending on your purpose, there are a number of places you can save the downloaded images:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0]
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
NSTemporaryDirectory()
If you want any retention rules like "keep only 10" or "delete after one day", you'll have to implement that in your code yourself. You could rely on file modification dates (from NSFileManager's
attributesOfItemAtPath:error:
), or keep the filename-to-date mapping in NSUserDefaults or Core Data, or just name the files to include the date.当谈到图像时,我建议将它们保存到磁盘,应用程序的沙箱中有 Documents 文件夹。您可以轻松地将文件名存储在用户默认值中,并在以后使用它来访问该文件。
如果您想知道图像是否“太旧”,您可以执行以下操作:
When it comes to images I recommend saving those to disk, there is the Documents folder in the application's sandbox. You could easily store the name of the file in the userdefaults and use it later to access the file.
If you like to find out if an image is "too old" you can do something like this:
我会使用优秀的 ASIHTTPRequest 库 首先下载图像:)
这取代了 NSURLConnection,并且您可以指定如何缓存图像 - 您只需从同一 URL 重新请求图像,ASIHTTPRequest 将返回缓存的图像。
I would use the excellent ASIHTTPRequest library to download the image in the first place :)
This replaces NSURLConnection and you can specify how to cache the images - you would just re-request the images from the same URL and ASIHTTPRequest would return the cached ones.