【问题标题】:CGRectMake for UIImageView?UIImageView的CGRectMake?
【发布时间】:2011-07-13 18:42:48
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: objective-c xcode uiimageview cgrect


    【解决方案1】:

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

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

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

    UIImageView 是用于显示 UIImage 的 UIView。

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

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

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

    UIImageView *multibtn = [[UIImageView alloc] initWithImage:...];
    

    【讨论】:

    • 对不起大家,我在发布后第二次意识到这一点,我太粗心了
    • @user842059:没问题。但是请不要全部大写,它读起来好像你在大喊大叫(并且伤害了我的耳朵)。
    • 哦,对不起,我已经做了 initWithImage,但在我发布后我意识到我所有的错误
    【解决方案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];
    

    【讨论】:

    • 对不起大家,我在发布后第二次意识到这一点,IM SO CARELLES
    【解决方案3】:

    CGRect,顾名思义,代表一个矩形,而不是图像,因此您必须将其分配给图像视图的一个属性,即矩形,在本例中为 frame

    【讨论】:

    • 对不起大家,我在发布后第二次意识到这一点,粗心
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多