以下代码从 iOS 11 返回一个图像对象path
并在 iOS 11 之前工作:
NSString *path = [anotherPath stringByAppendingPathComponent:file];
UIImage *image = [UIImage imageNamed:path];
但是在 iOS 11 中,image
返回null
. 这是 iOS 11 的错误吗?
以下代码从 iOS 11 返回一个图像对象path
并在 iOS 11 之前工作:
NSString *path = [anotherPath stringByAppendingPathComponent:file];
UIImage *image = [UIImage imageNamed:path];
但是在 iOS 11 中,image
返回null
. 这是 iOS 11 的错误吗?
+[UIImage imageNamed:]
记录为采用文件名,而不是路径。如果要从路径加载图像,请使用+[UIImage imageWithContentsOfFile:]
.
如果您在此处的路径指的是捆绑包中嵌套文件夹中的某些内容,则可以询问NSBundle
资源的路径,然后将其传递给+[UIImage imageWithContentsOfFile:]
. 这看起来像
NSString *path = [NSBundle.mainBundle pathForResource:@"foo" ofType:@"jpg" inDirectory:@"dir"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
正如@drewster 指出的那样,路径不正确;我有一个小写字符。但是,我注意到 iOS 8 忽略了这一点。