gpt4 book ai didi

ios - xcode View 不显示 url 图片

转载 作者:行者123 更新时间:2023-11-28 08:09:37 26 4
gpt4 key购买 nike

我正在学习斯坦福大学的 iOS 开发类(class),并将类(class)中运行良好的示例代码复制到我的 Xcode 中。该 View 只是不显示 url 图像,我已经将信息中的允许任意加载设置为"is"。屏幕上没有出现任何错误,但 View 没有显示任何内容。

  class ImageViewController: UIViewController

{

var imageURL: URL? {
didSet {
image = nil
if view.window != nil {
fetchImage()
}
}
}

private func fetchImage() {
if let url = imageURL {
let urlContents = try? Data(contentsOf: url)
if let imageData = urlContents {
image = UIImage(data: imageData)
}
}
}

override func viewDidLoad() {
super.viewDidLoad()
imageURL = DemoURL.stanford // for demo/testing purposes only


//This is the url image. It is in another swift file,the address is ( static let stanford = URL(string: "http://stanford.edu/about/images/intro_about.jpg",) I can open it in safari.

}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if image == nil { // we're about to appear on screen so, if needed,
fetchImage() // fetch image
}
}


fileprivate var imageView = UIImageView()
private var image: UIImage? {
get {
return imageView.image
}
set {
imageView.image = newValue
imageView.sizeToFit()
// careful here because scrollView might be nil
// (for example, if we're setting our image as part of a prepare)
// so use optional chaining to do nothing
// if our scrollView outlet has not yet been set

}
}
}

提前致谢。

最佳答案

DemoURL.swift 中的 URL 不存在...例如,您可以在 DemoURL.swift 中写入:

static let stanford = URL(string:"http://upload.wikimedia.org/wikipedia/commons/thumb/c/cd/Stanford_Oval_May_2011_panorama.jpg/800px-Stanford_Oval_May_2011_panorama.jpg")

关于ios - xcode View 不显示 url 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44072058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com