我是iOS和Xcode的新手.
我尝试加载图像
[UIImage imageNamed:@"imageName.png/jpg and so on"];
Run Code Online (Sandbox Code Playgroud)
但它只返回零.图像应该在我的项目包中,考虑到我可以从InterfaceBuilder中的拖放菜单中选择它,并通过"添加文件到"项目名"..."菜单将其添加到Xcode中.
我使用.png或.jpg图像没有区别,结果保持不变:它们通过IB工作,但如果我尝试自己运行imageNamed方法则不行.
用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删除,清理项目和将文件复制到包本身似乎对我有用.
我有一个包含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)
这是我的项目结构:
你能看到项目代码/结构有什么问题吗?