imageNamed is evil - How to use the function

江枫思渺然 提交于 2019-12-11 04:47:17

问题


- (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;
}

I got this code from http://www.alexcurylo.com/blog/2009/01/13/imagenamed-is-evil/ . Can someone tell me how to use this code. I need just a little help how to use this in place of imageNamed.


回答1:


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

then add "thumbnails" folder to ur resource folder then put ur png there

- (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;
}

example

add foo.png to resource folder //here create UIImageView object then

UIImageviewObject.image=[self thumbnailImage:@"foo.png"];



回答2:


The code uses a NSMutableDictionary *thumbnailCache to cache UIImage instances. The code assumes that in the app bundle, there's a directory thumbnails with scaled down versions of their images.

The method now first looks in the thumbnailCache dictionary whether the thumbnail for the given image (which is only a filename without full path, e. g. myimage.png). If the dictionary did not contain an image already, the image is loaded from the thumbnails directory (using imageWithContentsOfFile: instead of imageNamed:, since the authors claim the later causes trouble). The loaded image is then stored in the dictionary so the next time the app asks for the thumbnail, it can use the already loaded instance.

For this code to work correctly in your app, you need to add a thumbnails folder to your project. When you add it to your project, be sure to select "Create folder references for any added folders" instead of the default "Create groups for any added folders". Only then you will get a subdirectory in your app's main bundle, otherwise all files are put into the same top-level folder.

But the whole point is that the author claims:

  • Avoid [UIImage imageNamed:].
  • Instead, have a NSMutableDictionary.
  • Look up images in the dictionary.
    • If found, use that.
    • If not found, load image using [UIImage imageWithContentsOfFile:] to manually load the image and store it in the dictionary.



回答3:


thumbnailCache is NSMutableDictionary declared in header file and it should be initialized in .m init method or equivalent method.

If u have the images in the resources (with jpg format, else change the .jpg to .png in the code), then the line should be like

  NSString *thumbnailFile = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], fileName];

Instead of using

UIImage *thumbImage = [UIImage imageNamed:@"thumb.png"];

UIImage *thumbImage = [self thumbnailImage:@"thumb.png"];


来源:https://stackoverflow.com/questions/6623295/imagenamed-is-evil-how-to-use-the-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!