iphone开发中图像的路径是什么

Geo*_*F67 4 iphone objective-c

我有这个客观c代码片段:

UIImage *image = [ [UIImage alloc] initWithContentsOfFile:fileName];
Run Code Online (Sandbox Code Playgroud)

fileName设置为"file1.jpg"

但是,当我运行代码时,image被设置为nil.

我知道文件确实存在我猜它与路径有关.

我应该使用什么路径?

Jas*_*oco 14

最容易使用的是imageNamed:这样的:

UIImage* theImage = [UIImage imageNamed:@"file1.jpg"];
Run Code Online (Sandbox Code Playgroud)

如果initWithContentsOfFile:由于某种原因需要使用,则需要从包中获取路径,如下所示:

NSString* path = [[NSBundle mainBundle] pathForResource:@"file1" ofType:@"jpg"];
UIImage* theImage = [[UIImage alloc] initWithContentsOfFile:path];
Run Code Online (Sandbox Code Playgroud)