ios UIImage 不显示使用 Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24032371/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:22:42  来源:igfitidea点击:

UIImage not displaying using Swift

iosuiimageswift

提问by Joseph Mark

Code written in Swift for displaying UIImages works in the iOS 8.0 simulator but not on a phone running IOS 7.0 for some reason.

用 Swift 编写的用于显示 UIImages 的代码可以在 iOS 8.0 模拟器中运行,但由于某种原因不能在运行 IOS 7.0 的手机上运行。

let image1 = UIImage(named: "img1")
let imageview = UIImageView(image: image1)
self.view.addSubview(imageview)

let image2 = UIImage(named: "img2")
let button = UIButton(frame: CGRect(origin: CGPoint(x: 0, y: 100), size: image2.size)
button.setImage(image2, forState: UIControlState.Normal)
self.view.addSubview(button)

The images are in the right place with the right size and valid references, they're just not being displayed, except on the simulator running iOS 8.

图像位于正确的位置,具有正确的大小和有效的引用,它们只是没有显示,除非在运行 iOS 8 的模拟器上。

Is this a problem for anyone else?

这是其他人的问题吗?

EDIT:Deleting from Images.xcassets, cleaning the project and copying the files into the bundle itself seems to work for me.

编辑:从 Images.xcassets 中删除,清理项目并将文件复制到包本身似乎对我有用。

采纳答案by Ben

I tried your code in iOS 8-Simulator and on my iOS 7 device and it did what it should do. So no, in my case I don't see a problem.

我在 iOS 8-Simulator 和我的 iOS 7 设备上尝试了你的代码,它做了它应该做的。所以不,就我而言,我认为没有问题。

I changed img1 to img1.JPG (name of file) and img2 to img2.png. And I copied the images directly into the folder where the view controller is..

我将 img1 更改为 img1.JPG(文件名),将 img2 更改为 img2.png。我将图像直接复制到视图控制器所在的文件夹中。

回答by CNU

Inorder to display images you have to change the code in following way

为了显示图像,您必须按以下方式更改代码

let image1 = UIImage(named: "img1.jpg")
let imageview = UIImageView(image: image1)
self.view.addSubview(imageview)

let image2 = UIImage(named: "img2.jpg")
let button = UIButton(frame: CGRect(origin: CGPoint(x: 0, y: 100), size: image2.size)
button.setImage(image2, forState: UIControlState.Normal)
self.view.addSubview(button)

Remember swift is not supporting .png format images, It is supporting .jpg format.

记住 swift 不支持 .png 格式的图像,它支持 .jpg 格式。

回答by MrMins

You would check if the image was added to your framework, if that is unchecked you are not able to load the resource, but if you check it, the image will be loaded enter image description here

您将检查图像是否已添加到您的框架中,如果未选中,您将无法加载资源,但如果您选中它,图像将被加载 在此处输入图片说明

回答by Suragch

There seems to be a disconnect between the votes for the question and the other answers, so I will add a generic answer for how to display an image in Swift. That is what originally brought me here anyway.

问题的投票与其他答案之间似乎存在脱节,因此我将添加一个关于如何在 Swift 中显示图像的通用答案。无论如何,这就是最初将我带到这里的原因。

How to display an image in Swift

如何在 Swift 中显示图像

Use UIImageto get a reference to the image in the main bundle.

使用UIImage得到主捆绑到图像的参考。

let image = UIImage(named: "rocket.png")
imageView.image = image

Notes

笔记

  • imageViewis a UIImageView
  • In my tests, including or excluding the .png file extension did not make a different. It might be necessary to include if you are using multiple image formats, though.
  • Also in my tests, it did not make a difference whether the image was directly added to the project or was in Assets.xcassets.
  • If you need an image that is not in the main bundle, see this question.
  • According to the edit in the original question here, deleting the image and re-adding it to the bundle is what solved the problem.
  • imageView是一个 UIImageView
  • 在我的测试中,包含或排除 .png 文件扩展名并没有产生什么不同。但是,如果您使用多种图像格式,则可能需要包括在内。
  • 同样在我的测试中,图像是直接添加到项目中还是在 Assets.xcassets 中都没有区别。
  • 如果您需要不在主包中的图像,请参阅此问题
  • 根据此处原始问题中的编辑,删除图像并将其重新添加到包中是解决问题的方法。

回答by terminallyIll

I stored my images in the " Images.xcassets" folder and my images didn't appear on my device but they did appear on the simulator. In order to display the "Images.xcassets" images on my device (iPhone 5), I, the solution that worked for me was to copy bundle resources for the "Images.xcassets" folder

我将我的图像存储在“Images.xcassets”文件夹中,我的图像没有出现在我的设备上,但它们确实出现在了模拟器上。为了在我的设备(iPhone 5)上显示“Images.xcassets”图像,对我有用的解决方案是复制“Images.xcassets”文件夹的捆绑资源

To Copy Bundle Resources: Select Target>Build Phases>Copy Bundle Resources>Select "+" (Add Items)>Select "Images.xcassets

复制捆绑资源:选择目标>构建阶段>复制捆绑资源>选择“+”(添加项目)>选择“Images.xcassets

回答by paulo62

Swift 5:

斯威夫特 5:

The accepted answer no longer compiles for various reasons, so here is my updated version:

由于各种原因,接受的答案不再编译,所以这是我的更新版本:

    let image1 = UIImage(named: "img1.jpg")
    if let myImage1 = image1 {
        let imageview = UIImageView(image: myImage1)
        self.view.addSubview(imageview)
    } else {
        print ("img1 not loaded")
    }

    let image2 = UIImage(named: "img2.jpg")

    if let myImage2 = image2 {
        let button = UIButton(frame: CGRect(origin: CGPoint(x: 0, y: 100), size: myImage2.size))
        button.setImage(myImage2, for: .normal)
        self.view.addSubview(button)
    } else {
        print ("img2 not loaded")
    }