ios - NSBundle mainBundle pathForResource : Not working unless @2x included

标签 ios objective-c ipad filesystems nsbundle

我的项目中有名为:landing_load1@2x ...landing_load4@2x 的文件。 (没有非视网膜文件)

此代码有效:

for(int x=1; x < 5; x++){
    NSString * imageTitle = [NSString stringWithFormat:@"landing_load%i@2x", x];
    UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageTitle ofType:@"png"]];

    [loadingImages addObject:loadingImage];
}

但这行不通:

for(int x=1; x < 5; x++){
    NSString * imageTitle = [NSString stringWithFormat:@"landing_load%i", x];
    UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageTitle ofType:nil]];

    [loadingImages addObject:loadingImage];
}

这行不通:

for(int x=1; x < 5; x++){
      NSString * imageTitle = [NSString stringWithFormat:@"landing_load%i", x];
      UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageTitle ofType:nil]];

     [loadingImages addObject:loadingImage];
}

问题:我知道我不必显式调用 @2x 或指定文件类型,所以知道为什么它不起作用,除非我显式写出整个文件名吗?

注意:在 iPad 4 上测试

最佳答案

记录

NSString * imageTitle = [NSString stringWithFormat:@"landing_load%i", x];

意味着您有一对用于任何资源的图形文件 - “filename.png”和“filename@2x.png”。 或者,它可以仅用作“filename.png”的“别名” 如果只有“filename@2x.png”,则有两种可能的变体: 1. 使用类似的东西

NSString * imageTitle = [NSString stringWithFormat:"landing_load%i@2x", x];

或者拥有任何资源的两个文件(“filename.png”和“filename@2x.png”)。

关于ios - NSBundle mainBundle pathForResource : Not working unless @2x included,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22614148/

相关文章:

ios - 错误显示 : dyld_sim is not compatible with the loaded process

ios - 将自动布局与动态 UITextView、UIImageView 和 UILabel 高度一起使用

ios - Cell 在 iPad 上没有正确隐藏?

iphone - UISplitViewController 无法更改主视图 Controller ?

objective-c - iOS5页面类似 View

ios - 为 RideIntent 创建自定义 CLPlacemark

ios - 使用本地 url 通过 iOS 应用程序进行 ChromeCast 视频(错误)

ios - 从 NSMutableArray 创建一个 csv

ios - 具有 cocoapod 依赖项 : define in Podspec s. 依赖项的静态库,在 Podfile 中,还是两者都有?

iOS更新推送内容扩展