xcode UIImageView 的 CGRectMake?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6683904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CGRectMake for UIImageView?
提问by user842059
I am trying to use CGRectMake to assign a place and size to a uiimageview, here is the code;
我正在尝试使用 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];
}
So, as you can see if i press a button it should add a uiimageview with a picture. But for some reason, i get this error on the CGRectMake line: Assigning uiimageview to incompatible type CGRect, I thought a CGRect was a uiimage
所以,正如你所看到的,如果我按下一个按钮,它应该添加一个带有图片的 uiimageview。但出于某种原因,我在 CGRectMake 行上收到此错误:将 uiimageview 分配给不兼容的类型 CGRect,我以为 CGRect 是 uiimage
回答by Anomie
A CGRect, a UIImage, and a UIImageView are completely different things.
CGRect、UIImage 和 UIImageView 是完全不同的东西。
CGRect is a simple stucture defining a rectangle in some arbitrary coordinate space, using a point (origin) and size (size).
CGRect 是一个简单的结构,它使用点(原点)和大小(大小)在某个任意坐标空间中定义一个矩形。
A UIImage is an image and associated metadata.
UIImage 是图像和关联的元数据。
A UIImageView is a UIView that is intended for displaying a UIImage.
UIImageView 是用于显示 UIImage 的 UIView。
It looks like what you really want is to set the frame
property of your UIImageView, to instruct it as to where on the screen it should display:
看起来您真正想要的是设置frame
UIImageView的属性,以指示它应该在屏幕上显示的位置:
multibtn.frame = CGRectMake(159, 325, 438, 318);
Also, BTW, don't forget to initialize your UIImageView, by calling either initWithImage:
, initWithFrame:
, or the like on it. This is usually done at the same time as the allocation:
另外,顺便说一句,不要忘了初始化的UIImageView,通过调用initWithImage:
,initWithFrame:
等就可以了。这通常与分配同时完成:
UIImageView *multibtn = [[UIImageView alloc] initWithImage:...];
回答by Anomie
You didn't initialize your UIImageView
. Try calling -(id)initWithImage:(UIImage *)image;
givig nil
at first, and then setting both image
and frame
properties.
你没有初始化你的UIImageView
. 首先尝试调用-(id)initWithImage:(UIImage *)image;
givig nil
,然后设置image
和frame
属性。
UIImageView *multibtn = [[UIImageView alloc] initWithImage:nil];
multibtn.image = [UIImage imageNamed:@"multibuttonpic"];
multibtn.frame = CGRectMake(159, 325, 438, 318);
[self.view addSubview:multibtn];
or
或者
UIImageView *multibtn = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"multibuttonpic"]];
multibtn.frame = CGRectMake(159, 325, 438, 318);
[self.view addSubview:multibtn];
回答by omz
A CGRect
, as the name implies, represents a rectangle, not an image, so you have to assign it to a property of your image view that is a rectangle, the frame
in this case.
A CGRect
,顾名思义,代表一个矩形,而不是一个图像,所以你必须将它分配给你的图像视图的一个矩形属性,frame
在这种情况下。