ios 添加图像以查看
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9317911/
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
Add image to view
提问by OhDoh
How to add an image to an UIView? Here is what I tried so far:
如何将图像添加到 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];
回答by
Try a UIImageView
:
尝试一个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];
回答by Madhu
Method 1:
方法一:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image.jpg"]];
imgView.frame = CGRectOffset(imgView.frame, 100, 100);
[self.view addSubview:imgView];
Method 2:
方法二:
You can use the following process for xib files
您可以对xib文件使用以下过程
Step 1:You can Drag and drop the UIImageView from show the object library (Bottom right in xib)and place on view.
第 1 步:您可以从显示对象库(xib 中的右下角)中拖放 UIImageView 并将其放置在视图中。
Step 2:Copy the image to Xcode Project
第 2 步:将图像复制到 Xcode 项目
Step 3:Select the Image from the image drop down list which is present in right top corner.
第 3 步:从右上角的图像下拉列表中选择图像。
回答by Sohail
try this instead :
试试这个:
//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];`
回答by Francescu
Fyi
供参考
UIImage is not a subclass of UIView. UIImage is basically the imageData.
UIImage 不是 UIView 的子类。UIImage 基本上就是 imageData。
The main way to add an image to a view is to create an imageView from an image.
将图像添加到视图的主要方法是从图像创建一个 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];
This way you manipulate less "constants" numbers.
通过这种方式,您可以操纵较少的“常量”数字。