【问题标题】:How to display image in iphone simulator using objective C如何使用objective C在iphone模拟器中显示图像
【发布时间】:2014-03-28 08:12:57
【问题描述】:

我正在尝试使用目标 c 以编程方式在 iphone 模拟器中显示图像,我正在使用以下代码,但它没有在模拟器中显示任何内容

imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:@"A1.jpg"];
imageview.image=myimg;

【问题讨论】:

  • 你做对了。我的猜测是你搞砸了 UIImageView 以及你如何将它添加到你的 UIViewController 中。向我们展示您是如何做到的。
  • 我需要把我的图片放在哪里来显示它?
  • 试试这个教程:iosdeveloperzone.com/2011/05/23/… 这应该可以帮助您了解如何使用您想要的适当图像为您的 UIViewController 创建一个 UIImageView。记得将文件添加到您的项目中!
  • 使用这个 [self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"A1.png"]]];注意:使用 png 格式图片
  • colorWithPatternImage: 像重复纹理一样绘制图像,但事实并非如此。

标签: ios iphone objective-c uiimage


【解决方案1】:

您可以使用此代码

imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:@"A1.jpg"];
imageview.image=myimg;
imageview.frame = CGRectMake(50, 50, 150, 150); // pass your frame here
[self.view addSubview:imageview];

【讨论】:

    【解决方案2】:

    也许你忘了这个:

    imageView.frame = self.view.bounds;
    [self.view addSubview:imageview];
    

    【讨论】:

      【解决方案3】:

      如果您以编程方式创建 imageView,则必须使用下面的行将其添加到视图层次结构中

      [self.view addSubview:imageview];
      

      【讨论】:

        【解决方案4】:

        像这样将您的图像视图添加到视图中

        imageview = [[UIImageView alloc] initWithframe:CGRectMake(0, 0, 100, 200)];
        UIImage *myimg = [UIImage imageNamed:@"A1.jpg"];
        imageview.image=myimg;
        [self.view addSubview:imageview];
        

        【讨论】:

        • 我需要将图像保存在 iphone 模拟器的什么地方,以便显示它?
        • 您可以将图像 A1.jpg 保存在资源文件夹中,只需将图像从系统拖动到 xcode 项目即可。确保将项目复制到目标组的文件夹中
        【解决方案5】:

        首先您需要初始化您的 UIImageView 并将 UIImage 分配给它的图像属性。你已经在你的代码中做到了。然后您可以将框架设置为图像视图。

        imageview.frame = CGRectMake(50, 50, 150, 150);
        

        但在大多数情况下,将 sizeToFit 消息传递给您的图像视图会更有用。

        [imageview sizeToFit];
        

        所以它会改变它的框架以适应图像的大小。但是使用这种方法,您需要稍后将原点分配给图像视图的框架。所以最好的解决方案是在初始化时分配框架,然后传递 sizeToFit。

        UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 20.0, 0.0, 0.0)];
        UIImage *myimg = [UIImage imageNamed:@"A1.jpg"];
        imageview.image=myimg;
        [imageview sizeToFit];
        

        并且不要忘记将图像视图添加到视图层次结构中。

        [self.view addSubview:imageview];
        

        您还需要检查您的图像是否正确添加到您的项目中。您可以通过在 Project Navigator 中找到您的图像文件并在 File Inspector 中检查它的 Target Membership 来检查它。目标名称旁边应该有一个勾号。

        【讨论】:

          猜你喜欢
          • 2011-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-28
          • 2011-07-09
          • 2013-05-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多