添加图像到视图
如何添加图像到UIView?以下是我试过到目前为止:添加图像到视图
UIView *imageHolder = [[UIView alloc] initWithFrame:CGRectMake(40, 200, 280, 192)];
UIImage *image = [UIImage imageNamed:@"bloodymoon.jpg"];
[imageHolder addSubview:image]; // Error: Incompatible pointer types
[self.mainView addSubview:imageHolder];
尝试UIImageView
:
UIImageView *imageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(40, 200, 280, 192)];
UIImage *image = [UIImage imageNamed:@"bloodymoon.jpg"];
imageHolder.image = image;
// optional:
// [imageHolder sizeToFit];
[self.mainView addSubview:imageHolder];
似乎没有人喜欢使用'initWithImage:'(这实际上是指定的初始化)。 – 2012-02-16 22:16:41
他不想这样做。所以我想我会使用他最喜欢的initWithFrame:... – 2012-02-17 18:28:07
方法1:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image.jpg"]];
imgView.frame = CGRectOffset(imgView.frame, 100, 100);
[self.view addSubview:imgView];
方法2:
您可以使用下面的过程xib文件
第1步:您可以拖放UIImageView以显示对象库(xib中的右下角)并放置在视图中。
步骤2:将图像复制到xcode项目
步骤3:选择从所述图像的图像下拉列表这是目前在右上角。
在这里你误解了,OhDoh想以编程方式显示图像。 – 2013-10-12 10:59:05
据透露
的UIImage不是的UIView的子类。 UIImage基本上是imageData。
将图像添加到视图的主要方式是从图像创建一个imageView。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bloodymoon.jpg"]];
//imageView has a frame {0,0,imagewidth,imageheight}
//so just move it
imageView.frame = CGRectOffset(imageView.frame, 40, 200);
[self.mainView addSubview:imageView];
这样你操纵的“常量”数字就少了。
试试这个:
//set UiView background color to orange to find out the location on screen
UIView * view111 = [[UIView alloc ] initWithFrame:CGRectMake(50,50, 50, 50)];
view111.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view111];
UIImageView *imageImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"water1.png"]];`
// can set position to 0,0 (origin) instead of 10,10,
imageImg.frame = CGRectMake(10,10,30,30);
[view111 addSubview:imageImg];`
选择一个答案 – tjpaul 2015-04-23 19:27:17