1

我正在尝试使用 CGRectMake 为 uiimageview 分配位置和大小,这是代码;

     -(IBAction)done11 {

  [self performSelector:@selector(multibuttonvoid) withObject:nil afterDelay:2.0];



      }

  -(void)multibuttonvoid {

 UIImageView *multibtn = [UIImageView alloc];
multibtn.image = [UIImage imageNamed:@"multibuttonpic"];

multibtn = CGRectMake(159, 325, 438, 318);
  [self.view addSubview:multibtn];


      }

所以,正如你所看到的,如果我按下一个按钮,它应该添加一个带有图片的 uiimageview。但由于某种原因,我在 CGRectMake 行上收到此错误:Assigning uiimageview to incompatible type CGRect, I think a CGRect is a uiimage

4

3 回答 3

5

CGRect、UIImage 和 UIImageView 是完全不同的东西。

CGRect 是一个简单的结构,在任意坐标空间中定义一个矩形,使用点(原点)和大小(大小)。

UIImage 是一个图像和相关的元数据。

UIImageView 是用于显示 UIImage 的 UIView。

看起来您真正想要的是设置frameUIImageView 的属性,以指示它应该在屏幕上显示的位置:

multibtn.frame = CGRectMake(159, 325, 438, 318);

另外,顺便说一句,不要忘记通过调用 、 或类似方法来初始化您的initWithImage:UIImageView initWithFrame:。这通常与分配同时进行:

UIImageView *multibtn = [[UIImageView alloc] initWithImage:...];
于 2011-07-13T18:46:50.050 回答
2

你没有初始化你的UIImageView. 首先尝试调用-(id)initWithImage:(UIImage *)image;givig nil,然后设置imageframe属性。

UIImageView *multibtn = [[UIImageView alloc] initWithImage:nil];
multibtn.image = [UIImage imageNamed:@"multibuttonpic"];
multibtn.frame = CGRectMake(159, 325, 438, 318);
[self.view addSubview:multibtn];

或者

UIImageView *multibtn = [[UIImageView alloc]
                         initWithImage:[UIImage imageNamed:@"multibuttonpic"]];
multibtn.frame = CGRectMake(159, 325, 438, 318);
[self.view addSubview:multibtn];
于 2011-07-13T18:45:45.137 回答
1

A CGRect,顾名思义,代表一个矩形,而不是一个图像,所以你必须将它分配给你的图像视图的一个属性,即矩形,frame在这种情况下。

于 2011-07-13T18:45:10.793 回答