缓存 URL - 创建适当的 NSString 表示

发布于 2025-01-03 10:15:42 字数 225 浏览 2 评论 0原文

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

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

发布评论

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

评论(2

千年*琉璃梦 2025-01-10 10:15:42

我会使用散列:

  • 它会使文件名更具可读性
  • ,避免文件名长度问题

md5 应该完全适合您的目的。不幸的是,ios5-sdk 仅包含一个 C 字符串函数:

CC_MD5(in, in_len, out);

该函数包含在 中,那里还有其他哈希函数。

您可以在此处此处<找到有关如何将其包装在接受/返回 NSString 的函数中的信息< /a>.

I'd use hashing:

  • it'll make for much more readable file names
  • you avoid problems with file name length

md5 should be perfectly fine for your purposes. Unfortunately, the ios5-sdk contains only a C-String function for this:

CC_MD5(in, in_len, out);

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.

汹涌人海 2025-01-10 10:15:42

以下代码属于 ZDS_Shared。 resolveLocalURLForRemoteURL 接受远程 URL 并返回指向 iOS 文件系统上的文件的 URL。文件名将是一个字母数字字符串,与原始 URL 不同,但这应该不是问题。

https://github.com/ZarraStudios/ZDS_Shared/blob/master/ZSAssetManager .m#L185

- (NSURL*)resolveLocalURLForRemoteURL:(NSURL*)url
{
  if (!url) return nil;

  NSString *filename = [[url absoluteString] zs_digest];
  NSString *filePath = [[self cachePath] stringByAppendingPathComponent:filename];

  return [NSURL fileURLWithPath:filePath];
}

https://github.com/ZarraStudios/ZDS_Shared/blob/master/NSString %2BZSAdditions.m#L38

// NSString category
- (NSString*)zs_digest
{
  const char *cstr = [self cStringUsingEncoding:NSASCIIStringEncoding];
  return [[NSData dataWithBytes:cstr length:strlen(cstr)] zs_digest];
}

https://github.com/ZarraStudios/ZDS_Shared/blob/master/NSData %2BZSAdditions.m#L38

// NSData category
- (NSString*)zs_digest
{
  uint8_t digest[CC_SHA1_DIGEST_LENGTH];

  CC_SHA1([self bytes], [self length], digest);

  NSMutableString* outputHolder = [[NSMutableString alloc] initWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

  for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
    [outputHolder appendFormat:@"%02x", digest[i]];
  }

  NSString *output = [outputHolder copy];
  MCRelease(outputHolder);

  return [output autorelease];
}

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

- (NSURL*)resolveLocalURLForRemoteURL:(NSURL*)url
{
  if (!url) return nil;

  NSString *filename = [[url absoluteString] zs_digest];
  NSString *filePath = [[self cachePath] stringByAppendingPathComponent:filename];

  return [NSURL fileURLWithPath:filePath];
}

https://github.com/ZarraStudios/ZDS_Shared/blob/master/NSString%2BZSAdditions.m#L38

// NSString category
- (NSString*)zs_digest
{
  const char *cstr = [self cStringUsingEncoding:NSASCIIStringEncoding];
  return [[NSData dataWithBytes:cstr length:strlen(cstr)] zs_digest];
}

https://github.com/ZarraStudios/ZDS_Shared/blob/master/NSData%2BZSAdditions.m#L38

// NSData category
- (NSString*)zs_digest
{
  uint8_t digest[CC_SHA1_DIGEST_LENGTH];

  CC_SHA1([self bytes], [self length], digest);

  NSMutableString* outputHolder = [[NSMutableString alloc] initWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

  for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
    [outputHolder appendFormat:@"%02x", digest[i]];
  }

  NSString *output = [outputHolder copy];
  MCRelease(outputHolder);

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