Is there a way to take advantage of the device modifiers ~ipad, ~iphone, @2x, for images stored in the cache or documents. As far as I can tell it only works if the files are stored in the main bundle.
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"];
I'm quite sure that the imageWithContentsOfFile:
method does not analyze the file name. I had the same issue back on iPhone when I tried to load some @2x images which were not part of the bundle. In the end I had to use the initWithCGImage:scale:orientation:
to be able to set the scale properly. Based on that, I would be surprised if it could handle the ~ipad
suffix.