简体   繁体   English

Swift:添加UIImage吗?

[英]Swift: Adding UIImage?

How do I add an image via Swift with the UIImage command? 如何使用UIImage命令通过Swift添加图像? This is my current code: 这是我当前的代码:

var imageView = UIImageView(frame: CGRectMake(100, 100, 150, 150));     
myPic = UIImage(named: "bowling");
imageView.image = myPic;
self.view.addSubview(imageView);

It does not show anything, and this is under viewDidLoad() , which should load by default. 它不显示任何内容,它在viewDidLoad()下,默认情况下应加载。

Thanks! 谢谢! :) :)

It's better to put the image view on your view controller in IB (Interface Builder), connect an outlet, and then install an image into it in code. 最好将图像视图放在IB(接口生成器)中的视图控制器上,连接插座,然后用代码在其中安装图像。

Getting the image to appear in the correct place and at the right size requires different steps depending on whether you're using AutoLayout or AutoResizingMasks (aka old style "struts-and-springs" layout.) 要使图像显示在正确的位置并以正确的尺寸显示,需要执行不同的步骤,具体取决于您使用的是AutoLayout还是AutoResizingMasks(又称旧样式“ struts-springs”布局)。

If you're using AutoLayout, which is the default, you need to add constraints that give your view a size and a location in it's view. 如果使用默认的自动版式,则需要添加约束,以赋予视图大小和视图位置。 At a minimum you should set the view's height and width and set it's vertical and horizontal center relative to it's superview's center. 至少应设置视图的高度和宽度,并设置其相对于父视图中心的垂直和水平中心。 (Note that you have to add it to its superview before adding constraints.) (请注意,在添加约束之前,必须将其添加到其超级视图。)

Adding constraints is easier in IB than in code. 在IB中添加约束比在代码中更容易。

Your second line should read let myPic = UIImage(named: "bowling"); 您的第二行应为let myPic = UIImage(named: "bowling"); , since you have not defined myPic yet, but it's much easier to add an image view in the storyboard, connect an outlet to the view controller code, then define what image the view should contain. ,因为您尚未定义myPic,但在情节提要中添加图像视图,将插座连接到视图控制器代码,然后定义视图应包含的图像要容易得多。

silver_belt answered this for me. silver_belt为我回答了这个问题。 Silly me, I never added the picture to the project hierarchy! 愚蠢的我,我从未将图片添加到项目层次结构中! Thanks a million guys! 谢谢百万人! :) :)

"Can you confirm that you have an image named "bowling" in an Asset Catalog or added as a resource to your project? I know it might sound silly... but also keep in mind filenames are case-sensitive on devices (not in the simulator)." “您可以确认您在资产目录中是否有一个名为“保龄球”的图像或作为资源添加到您的项目中吗?我知道这听起来很愚蠢……但请记住,文件名在设备上是区分大小写的(不是模拟器)。”

Make sure your "bowling" image is added to your project (as a resource, or in an Asset Catalog). 确保将“保龄球”图像添加到您的项目中(作为资源或在资产目录中)。

Keep in mind filenames on devices are case-sensitive, while the simulator is not. 请记住,设备上的文件名区分大小写,而模拟器不区分大小写。

Your UIImage and UIImageView code is otherwise fine. 否则,您的UIImage和UIImageView代码就可以了。

Try this:- 尝试这个:-

    var imageViewBowline: UIImageView = UIImageView()
    imageViewBowline.frame = CGRectMake(100, 100, 150, 150)
    var myPicture = UIImage(named: "bowling.jpg")
    imageViewBowline.image = myPicture 
    self.view.addSubview (imageViewBowline)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM