相关疑难解决方法(0)

UIImage imageNamed返回nil

我是iOS和Xcode的新手.

我尝试加载图像

[UIImage imageNamed:@"imageName.png/jpg and so on"];
Run Code Online (Sandbox Code Playgroud)

但它只返回零.图像应该在我的项目包中,考虑到我可以从InterfaceBuilder中的拖放菜单中选择它,并通过"添加文件到"项目名"..."菜单将其添加到Xcode中.

我使用.png或.jpg图像没有区别,结果保持不变:它们通过IB工作,但如果我尝试自己运行imageNamed方法则不行.

xcode uiimage imagenamed ios

46
推荐指数
6
解决办法
6万
查看次数

UIImage不使用Swift显示

用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)
Run Code Online (Sandbox Code Playgroud)

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

这对其他人来说是个问题吗?

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

uiimage ios swift

23
推荐指数
3
解决办法
5万
查看次数

从捆绑包中的资源文件夹加载图像

我有一个包含assets文件夹的包.我已经阅读了有关使用的堆栈的所有答案UIImage(named: "drop_arrow", inBundle: bundle, compatibleWithTraitCollection: nil)(好吧,它快速3等效)

path = Bundle.main.path(forResource: "LiveUI", ofType: "bundle")
if path != nil { // path with bundle is found
     let bundle: Bundle = Bundle.init(path: path!)! // bundle is there
     let image: UIImage? = UIImage(named: "HomePlayButton", in: bundle, compatibleWith: nil)
     // HomePlayButton exists in the bundle/asset folder
     // but image is nil
}
Run Code Online (Sandbox Code Playgroud)

这是我的项目结构:

在此输入图像描述

你能看到项目代码/结构有什么问题吗?

UPDATE!...图像为所有分辨率设置为通用: 在此输入图像描述

ios swift swift3

8
推荐指数
1
解决办法
7017
查看次数

标签 统计

ios ×3

swift ×2

uiimage ×2

imagenamed ×1

swift3 ×1

xcode ×1