缓存 URL - 创建适当的 NSString 表示
我想将从 URL 下载的图像缓存到本地文件系统中。
我不知道的是,如何从 URL 创建与 iOS 文件系统支持的字符兼容的 NSString
。例如,如果我尝试创建一个名称中包含 :
的文件,它将失败。
我应该遵循哪些步骤来创建这个 NSString
?简单的哈希是最好的方法吗?如果是这样,iOS 中有哪些哈希例程可供我使用?
I want to cache an image I download from a URL in the local filesystem.
What I don't know, is how can I create from the URL a NSString
that is compatible with the characters the iOS file system supports. For instance, if I try to create a file with a :
in the name, it will fail.
What steps should I follow to create this NSString
? Is a simple hash the best way to go? If so, what hash routine is available in iOS that I can use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用散列:
md5 应该完全适合您的目的。不幸的是,ios5-sdk 仅包含一个 C 字符串函数:
该函数包含在
中,那里还有其他哈希函数。您可以在此处此处<找到有关如何将其包装在接受/返回 NSString 的函数中的信息< /a>.
I'd use hashing:
md5 should be perfectly fine for your purposes. Unfortunately, the ios5-sdk contains only a C-String function for this:
The function is contained in
<CommonCrypto/CommonDigest.h>
, there are other hash functions there, too.You can find infos on how to wrap this up in a function that takes/returns an NSString here.
以下代码属于 ZDS_Shared。
resolveLocalURLForRemoteURL
接受远程 URL 并返回指向 iOS 文件系统上的文件的 URL。文件名将是一个字母数字字符串,与原始 URL 不同,但这应该不是问题。https://github.com/ZarraStudios/ZDS_Shared/blob/master/ZSAssetManager .m#L185
https://github.com/ZarraStudios/ZDS_Shared/blob/master/NSString %2BZSAdditions.m#L38
https://github.com/ZarraStudios/ZDS_Shared/blob/master/NSData %2BZSAdditions.m#L38
The following code belongs to ZDS_Shared.
resolveLocalURLForRemoteURL
accepts a remote URL and returns a URL pointing to the file on the iOS filesystem. The filename will be an alphanumeric string that doesn't resemble the original URL, but that shouldn't be a problem.https://github.com/ZarraStudios/ZDS_Shared/blob/master/ZSAssetManager.m#L185
https://github.com/ZarraStudios/ZDS_Shared/blob/master/NSString%2BZSAdditions.m#L38
https://github.com/ZarraStudios/ZDS_Shared/blob/master/NSData%2BZSAdditions.m#L38