保存/重新加载图像,并删除旧图像

发布于 2024-11-08 12:16:16 字数 474 浏览 0 评论 0原文

我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煞人兵器 2024-11-15 12:16:16

根据您的目的,您可以在多个位置保存下载的图像:

  • 如果您想存储文件直到删除它,请写入文档目录: [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]
  • 如果您想存储文件直到删除它并且您可能曾经使用iTunes 文件共享 并且不希望共享这些特定文件,请写入应用程序支持目录: [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:

  • If you want to store the file until you delete it, write to the documents directory: [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  • If you want to store the file until you delete it and you might ever use iTunes file sharing and don't want these particular files shared, write to the Application Support directory: [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  • If you want to allow the system to delete it if the device is running low on space (and don't care if it is saved when the device is backed up), use the caches directory: [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  • If you're just saving it temporarily while you process it and will delete it right away, use the temporary directory: 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.

空名 2024-11-15 12:16:16

当谈到图像时,我建议将它们保存到磁盘,应用程序的沙箱中有 Documents 文件夹。您可以轻松地将文件名存储在用户默认值中,并在以后使用它来访问该文件。

如果您想知道图像是否“太旧”,您可以执行以下操作:

int aDayInSeconds = 60 * 60 * 24;

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fileName 
                                                                            error:nil];
NSDate *creationDate = [attributes objectForKey:@"NSFileCreationDate"];

if ([[NSDate date] timeIntervalSinceDate:creationDate] > aDayInSeconds) {
    // Delete, reload, whatever you need.
}

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:

int aDayInSeconds = 60 * 60 * 24;

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fileName 
                                                                            error:nil];
NSDate *creationDate = [attributes objectForKey:@"NSFileCreationDate"];

if ([[NSDate date] timeIntervalSinceDate:creationDate] > aDayInSeconds) {
    // Delete, reload, whatever you need.
}
笔芯 2024-11-15 12:16:16

我会使用优秀的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文