gpt4 book ai didi

ios - 为什么在使用该代码时图像不在 UIButton 中?

转载 作者:行者123 更新时间:2023-11-28 14:56:21 25 4
gpt4 key购买 nike

为什么在使用该代码时图像不在 UIButton 中?我有一个图像 URL,可以在我的应用中使用该图像。

//Show Image from URL
let url = URL(string:"https://static.pexels.com/p…/247932/pexels-photo-247932.jpeg")
let session = URLSession.shared
session.dataTask(with: url!, completionHandler: { (data, response, error) in
if (error == nil) && (data != nil) {
DispatchQueue.main.async {
let img = UIImage(data: data!)
print(img!)
self.otlBtnTakeImage.imageView?.image = UIImage(data: data!)
}
}
}).resume()

最佳答案

您可以使用此代码在 UIButton 中使用和显示图像。

let url = URL(string:"https://static.pexels.com/photos/247932/pexels-photo-247932.jpeg")
let session = URLSession.shared
session.dataTask(with: url!, completionHandler: { (data, response, error) in
if (error == nil) && (data != nil) {
DispatchQueue.main.async {
let img = UIImage(data: data!)
print(img!)
self.otlBtnTakeImage.setImage(img, for: .normal)
}
}
}).resume()

关于ios - 为什么在使用该代码时图像不在 UIButton 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49293583/

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