iphone - imageNamed是邪恶的-如何使用该功能

标签 iphone objective-c ios imagenamed

- (UIImage*)thumbnailImage:(NSString*)fileName
{
   UIImage *thumbnail = [thumbnailCache objectForKey:fileName];

   if (nil == thumbnail)
   {
      NSString *thumbnailFile = [NSString stringWithFormat:@"%@/thumbnails/%@.jpg", [[NSBundle mainBundle] resourcePath], fileName];
      thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile];
      [thumbnailCache setObject:thumbnail forKey:fileName];
   }
   return thumbnail;
}

我从http://www.alexcurylo.com/blog/2009/01/13/imagenamed-is-evil/获得了这段代码。有人可以告诉我如何使用此代码。我只需要一点帮助,如何使用它代替imageNamed。

最佳答案

NSMutableDictionary *thumbnailCache=[[NSMutableDictionary alloc]init];

然后将“thumbnails”文件夹添加到您的资源文件夹中,然后将ur png放在那里
- (UIImage*)thumbnailImage:(NSString*)fileName
{
   UIImage *thumbnail = [thumbnailCache objectForKey:fileName];

   if (nil == thumbnail)
   {
      NSString *thumbnailFile = [NSString stringWithFormat:@"%@/thumbnails/%@.jpg", [[NSBundle mainBundle] resourcePath], fileName];
      thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile];
      [thumbnailCache setObject:thumbnail forKey:fileName];
   }
   return thumbnail;
}



将foo.png添加到资源文件夹
//在这里创建UIImageView对象
UIImageviewObject.image=[self thumbnailImage:@"foo.png"];

关于iphone - imageNamed是邪恶的-如何使用该功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6623295/

相关文章:

ios - 找不到设备支持文件

iphone - 谷歌搜索政策

objective-c - 发送 NSCFCalendar nil NSDate 讽刺错误

iphone - 有条件地禁用 iOS 应用程序的后台?

ios - 如何使用uiviewcontroller实现滑出式菜单?

iphone - CGFloat 作为属性

html - iOS safari 搞乱了输入类型=日期

ios - Xcode-beta 8.无法创建核心数据

iphone - 如果在全局范围内部署应用程序,那么中国、日本、韩国等国家呢?

iphone - 构建 openears 兼容的语言模型