代码之家  ›  专栏  ›  技术社区  ›  GeoffreyF67

在iphone开发中,图像的发展道路是什么

  •  4
  • GeoffreyF67  · 技术社区  · 16 年前

    UIImage *image = [ [UIImage alloc] initWithContentsOfFile:fileName];
    

    文件名设置为“file1.jpg”

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

    我应该使用什么路径?

    3 回复  |  直到 16 年前
        1
  •  14
  •   Jason Coco superfell    16 年前

    imageNamed:

    UIImage* theImage = [UIImage imageNamed:@"file1.jpg"];
    

    如果你需要使用 initWithContentsOfFile: 出于某些原因,您需要从捆绑包中获取路径,如下所示:

    NSString* path = [[NSBundle mainBundle] pathForResource:@"file1" ofType:@"jpg"];
    UIImage* theImage = [[UIImage alloc] initWithContentsOfFile:path];
    
        2
  •  0
  •   DevDevDev    16 年前

    UIImage* image = [[UIImage imageNamed:fileName] retain];
    
        3
  •  0
  •   Devin Ceartas    16 年前

    当然,图像必须在捆绑包中——如果您在XCode中将其拖到项目中并从选项中选择“复制资源”,它应该会发现它是正确的。