Objective c 在文档或缓存文件夹中使用iOS文件名设备修饰符(~ipad、~iphone)
有没有办法利用设备修饰符~ipad、~iphone、@2x来处理存储在缓存或文档中的图像。据我所知,只有当文件存储在主捆绑包中时,它才起作用Objective c 在文档或缓存文件夹中使用iOS文件名设备修饰符(~ipad、~iphone),objective-c,ios,uiimage,imagenamed,Objective C,Ios,Uiimage,Imagenamed,有没有办法利用设备修饰符~ipad、~iphone、@2x来处理存储在缓存或文档中的图像。据我所知,只有当文件存储在主捆绑包中时,它才起作用 NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = ([cachePaths count] > 0) ? [cachePaths objectAtIndex:
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"];
我确信
imageWithContentsOfFile:
方法不会分析文件名。当我尝试加载一些不属于捆绑包的@2x图像时,我在iPhone上遇到了同样的问题。最后,我不得不使用initWithCGImage:scale:orientation:
来正确设置比例。基于此,如果它能处理~ipad
后缀,我会感到惊讶。目前看来上述情况是正确的。最后我自己检查了缓存目录中的文件名(~ipad、~iphone、@2x~ipad、@2x~iphone)。