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