Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当使用该代码时,为什么图像不在UIButton中?_Ios_Swift_Session - Fatal编程技术网

Ios 当使用该代码时,为什么图像不在UIButton中?

Ios 当使用该代码时,为什么图像不在UIButton中?,ios,swift,session,Ios,Swift,Session,当使用该代码时,为什么图像不在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:

当使用该代码时,为什么图像不在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()
像这样设置图像

button.setImage(img, for: .normal) // To set image with title.
button.setBackgroundImage(img, for: .normal) // set btn background image.

使相同代码异步运行而不阻塞UI的简单方法是使用GCD:

let url = URL(string: image.url)
DispatchQueue.global().async {
    let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
    DispatchQueue.main.async {
        self.otlBtnTakeImage.setImage = UIImage(data: data!)
    }
}

上述更改可能会解决您的问题,并且按钮中的显示图像可以这样设置UIButton的图像

let image = UIImage(named: "imagename.png")
yourButton.setBackgroundImage(image, for: .normal)
yourButton.setImage(image, for: .normal)

您可以使用此代码在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()
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()