【问题标题】:Using iOS filename Device Modifiers (~ipad, ~iphone) in Documents or Caches folder在 Documents 或 Caches 文件夹中使用 iOS 文件名设备修饰符(~ipad、~iphone)
【发布时间】:2012-05-12 04:55:18
【问题描述】:

有没有办法利用设备修饰符 ~ipad、~iphone、@2x 来处理存储在缓存或文档中的图像。据我所知,它只有在文件存储在主包中时才有效。

NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = ([cachePaths count] > 0) ? [cachePaths objectAtIndex:0] : nil;
NSString *cacheResourcesPath = [cachePath stringByAppendingPathComponent:@"Resources"];

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;
NSString *documentsResourcesPath = [documentPath stringByAppendingPathComponent:@"Resources"];

    // SUCCESS (/caches/resources)
    UIImage *image = [UIImage imageWithContentsOfFile:[cacheResourcesPath stringByAppendingPathComponent:@"image~ipad.png"]];

    // FAIL (/caches/resources)
    UIImage *image = [UIImage imageWithContentsOfFile:[cacheResourcesPath stringByAppendingPathComponent:@"image.png"]];

    // SUCCESS (/documents)
    UIImage *image = [UIImage imageWithContentsOfFile:[documentsResourcesPath stringByAppendingPathComponent:@"image~ipad.png"]];

    // FAIL (/documents)
    UIImage *image = [UIImage imageWithContentsOfFile:[documentsResourcesPath stringByAppendingPathComponent:@"image.png"]];

    // SUCCESS (main bundle)
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"]];

    // SUCCESS (main bundle)
    UIImage *image = [UIImage imageNamed:@"image"];

【问题讨论】:

    标签: objective-c ios uiimage imagenamed


    【解决方案1】:

    我很确定imageWithContentsOfFile: 方法不会分析文件名。当我尝试加载一些不属于捆绑包的 @2x 图像时,我在 iPhone 上遇到了同样的问题。最后我不得不使用initWithCGImage:scale:orientation: 才能正确设置比例。基于此,如果它可以处理~ipad 后缀,我会感到惊讶。

    【讨论】:

    • 目前看来上述情况属实。我最终自己检查了缓存目录中文件的文件名(~ipad、~iphone、@2x~ipad、@2x~iphone)。
    猜你喜欢
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多