下载图像未显示iOS Swift

时间:2017-08-23 10:56:49

标签: ios swift image request nsurl

我正在尝试下载图像并根据不同的条件显示它们。如果我只是简单地下载主线程中的图像,代码就有效,但我现在正试图通过后端下载它们。

我的控制台很干净,所以我没有收到任何错误 - 但我的图像没有显示?这是我第一次尝试后端图像下载,并且当我来自objective-c时也做得很快。

有人可以查看下面的代码并告诉我这是什么问题吗?

//FizzBuzz Image Download
weak var fizzBuzzImage: UIImage!
let fizzBuzzString = "https://emkaydeum.files.wordpress.com/2015/08/fizzbuzz.png"
var fizzBuzzURL:NSURL!

//Fizz Image Download
weak var fizzImage: UIImage!
let fizzString = "https://vignette2.wikia.nocookie.net/leagueoflegends/images/b/b0/Fizz_Render.png/revision/latest?cb=20151205185848"
var fizzURL:NSURL!

//Buzz Image Download
weak var buzzImage: UIImage!
let buzzString = "https://vignette2.wikia.nocookie.net/disney/images/b/bc/Buzz_Disney_INFINITY_Render.png/revision/latest?cb=20140605182818"
var buzzURL:NSURL!

override func viewDidLoad() {
    super.viewDidLoad()

    fizzBuzzURL = NSURL(string: fizzBuzzString)
    fizzURL = NSURL(string: fizzString)
    buzzURL = NSURL(string: buzzString)
}

@IBAction func checkTextField(_ sender: UITextView){

    enteredValue = userInput.text

    let checkIfEmpty = userInput.text?.isEmpty

    if (checkIfEmpty == false) {
    let multipleOf3:Int = Int(enteredValue)!%3
    let multipleOf5:Int = Int(enteredValue)!%5

        if (multipleOf3 == 0 && multipleOf5 == 0) {
            fizzBuzz.text = "FizzBuzz"
            URLSession.shared.dataTask(with: (fizzBuzzURL as URL), completionHandler: {(fizzBuzzData, resp, error) -> Void in
                if (error != nil && fizzBuzzData != nil){
                    OperationQueue.main.addOperation ({
                        self.displayImage.image = UIImage(data: fizzBuzzData!)
                    })
                }
                }).resume()}
        else if (multipleOf5 == 0){
            fizzBuzz.text = "Buzz"
            URLSession.shared.dataTask(with: (buzzURL as URL), completionHandler: {(buzzData, resp, error) -> Void in
                if (error != nil && buzzData != nil){
                    OperationQueue.main.addOperation ({
                        self.displayImage.image = UIImage(data: buzzData!)
                })
                }
                }).resume()}
        else if (multipleOf3 == 0){
            fizzBuzz.text = "Fizz"
            URLSession.shared.dataTask(with: (fizzURL as URL), completionHandler: {(fizzData, resp, error) -> Void in
                if (error != nil && fizzData != nil){
                    OperationQueue.main.addOperation ({
                        self.displayImage.image = UIImage(data: fizzData!)
                })
                }
                }).resume()}
        else {
            fizzBuzz.text = "Incorrect Value"
            displayImage.image = nil
        }
    }
}

2 个答案:

答案 0 :(得分:1)

请检查您的IF条件。你写过

if (error != nil && fizzBuzzData != nil){
                    OperationQueue.main.addOperation ({
                        self.displayImage.image = UIImage(data: fizzBuzzData!)
                    })
                }

从未执行过。请按以下说明

 if (error == nil && fizzBuzzData != nil){
                    OperationQueue.main.addOperation ({
                        self.displayImage.image = UIImage(data: fizzBuzzData!)
                    })
                }

答案 1 :(得分:0)

uiimageview contentMode是scaleAspectFill

和clipsToBounds true