当高分辨率版本 (@2x) 也存在时,我在尝试在 iOS 上强制加载某些资源的低分辨率版本时遇到了很多麻烦。
我想这样做的原因很简单:我的应用程序向用户显示一堆全屏图像(旧设备上为 320x480,Retina 设备上为 640x960),用户可以将其中一些上传到 TwitPic。我想将上传的图像统一为 320x480,而不考虑设备(为了保持一致性,因为该尺寸在网络浏览器中没问题)。
我发现无论我使用什么 UIImage 方法,当资源文件的高分辨率版本存在时它都会被加载(即使您传递了完整的文件路径):UIImage 会比您聪明。但我找到了一种超越 UIImage 的方法:
- (UIImage*) lowResImage{
NSString* path = [[NSBundle mainBundle] pathForResource:_fileName
ofType:@"png"
inDirectory:_resourceSubdirectory];
path = [path stringByReplacingOccurrencesOfString:@"@@2x" withString:@""];
NSData* imageData = [[NSData alloc] initWithContentsOfFile:path];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[imageData release];
return [image autorelease];
}
以上代码在第 4 代 iPod touch 上运行良好。
有人提出更好的方法吗?
最佳答案
构建您的路径,打开数据,然后使用数据初始化图像,如下例所示。
NSString *dataPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:_fileName];
NSData *data = [[NSData alloc] initWithContentsOfFile:dataPath];
UIImage *image3 = [UIImage imageWithData:data];
关于ios - 当@2x-version 也存在时,在 iOS 中有选择地加载低分辨率资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7226529/