【问题标题】:Where should i put my images in my iPhone project?我应该将我的图像放在我的 iPhone 项目中的什么位置?
【发布时间】:2012-06-06 10:56:42
【问题描述】:

我是 iPhone 开发新手。

现在我有一个项目,它的目录结构如下所示:

Tutorial
       \__Tutorial\
                 \__1.png
                 \__TutorialViewController.h
                 \__TutorialViewController.m
                 \__TutorialViewController.xib
                 \__Supporting Files\
       \__Frameworks\
       \__Products\

我尝试使用 [UIImage imageNamed:@"1.png"] 将图像渲染到视图:

 UIImage *image = [UIImage imageNamed:@"1.png"];
 imageView.image = image;
 [image release];

但是图片没有显示出来。

所以我想知道我应该把图片放在哪里?

另外,如果我有很多图像(比如 1000 个数字),应该怎么办?


这是我的代码:

#import "TutorialViewController.h"

@implementation TutorialViewController
@synthesize labelText;
@synthesize imageView;

-(void) click:(id)sender {
    NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
    NSString *newText = [[NSString alloc]initWithFormat:@"%@",titleOfButton];
    // Change Image When Clicking Color Button
    if([titleOfButton isEqualToString:@"Blue"]){
        NSLog(@"Blue");
        imageView.image = [UIImage imageNamed:@"a.png"];
    }else{
        imageView.image = [UIImage imageNamed:@"b.png"];;
        NSLog(@"Else");
    }
    labelText.text = newText;
    [newText release];
}

【问题讨论】:

  • 尝试添加到支持文件夹中
  • 之前不分配镜像是无法释放的。你如何管理 imageView?通过代码还是 xib?
  • 不 [UIImage imageNamed:@"1.png"] 分配内存本身吗?我的 imageView 被定义为 IBOutlet 并从 xib 连接到 FileOwner。
  • @SimonePistecchia 我不分配它,但它不会抛出异常.....
  • imageNamed 返回一个自动释放的对象,因此您不必释放它。深入了解this。

标签: iphone objective-c ios xcode ios4


【解决方案1】:

您应该创建一个名为 Resources 的文件夹并在其中添加图像。

UIImage *image = [UIImage imageNamed:@"IMG_0270.PNG"];
imgView_.image = image;

我使用了上面的代码,它在我的系统上运行良好。可能是你没有在界面生成器中连接 imgView 的出口。

编辑:

我发现的问题是您没有将其添加为子视图。您应该像这样更改代码:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];

NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:@"%@", titleOfButton];

// Change Image When Clicking Color Button
if([titleOfButton isEqualToString:@"Blue"]) {
    NSLog(@"Blue");
    imageView.image = [UIImage imageNamed:@"IMG_0270.png"];
} else {
    imageView.image = [UIImage imageNamed:@"b.png"];
    NSLog(@"Else");
}

[imageView setFrame:CGRectMake(10, 10, 230, 123)];
[self.view addSubview:imageView];

// labelText.text = newText;
[newText release];

【讨论】:

  • @Neptune 我找不到在我的 xcode 4.1 中创建文件夹的方法。
  • @Neptune 同样,您在自动释放的对象上调用release,您不能,您不是该对象的所有者。现在它工作正常,但将来可能会使您的应用程序崩溃。
  • @MrROY 在xCode中右键项目名称,选择New Group
  • @Mat 好的。我已经删除了发布声明
  • @Neptune 我创建了资源,但它不起作用...它与支持文件相同吗?
【解决方案2】:

你的代码没有prbm。但有时图像损坏,由于这个原因它不显示,尝试一些其他图像,如果图像没有添加到捆绑路径添加在

项目目标>构建阶段>复制捆绑资源

。可能会有所帮助。

【讨论】:

    【解决方案3】:

    你对这个说法有问题

    [image release];
    

    记住何时使用发布的最简单方法是NARC关键字:)

    永远记得在 4 种情况下进行释放调用。

    N 新

    一个分配

    R 保留

    C 复制

    【讨论】:

    • NARC,仅在没有 ARC 时使用发布:D
    【解决方案4】:

    尝试删除方法的第一行,如果您已在 xib 中连接了UIImageView,则无需重新分配它。此外,请致电 self.imageView.image 而不是 imageView.image。

    【讨论】:

      猜你喜欢
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 2012-08-10
      • 2018-06-27
      相关资源
      最近更新 更多