iphoneobjective-ciosimagenamed

imageNamed is evil - How to use the function


- (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.


Solution

  • 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"];