1

有没有办法利用设备修饰符〜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"];
4

1 回答 1

0

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

于 2012-05-02T17:15:23.667 回答