Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在文档或缓存文件夹中使用iOS文件名设备修饰符(~ipad、~iphone)_Objective C_Ios_Uiimage_Imagenamed - Fatal编程技术网

Objective c 在文档或缓存文件夹中使用iOS文件名设备修饰符(~ipad、~iphone)

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:

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

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

目前看来上述情况是正确的。最后我自己检查了缓存目录中的文件名(~ipad、~iphone、@2x~ipad、@2x~iphone)。