下载图片未显示 iOS Swift
Download image not being displayed iOS Swift
我正在尝试下载图像并根据不同的条件显示它们。如果我只是在主线程中下载图像,代码就可以工作,但我现在正尝试通过后端下载它们。
我的控制台是干净的,所以我没有收到任何错误 - 但是我的图像没有显示?这是我第一次尝试进行后端图像下载,同时也进行了 swift,因为我来自 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
}
}
}
设置
uiimageview contentMode 是 scaleAspectFill
和 clipsToBounds 为真
请检查您的 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!)
})
}
我正在尝试下载图像并根据不同的条件显示它们。如果我只是在主线程中下载图像,代码就可以工作,但我现在正尝试通过后端下载它们。
我的控制台是干净的,所以我没有收到任何错误 - 但是我的图像没有显示?这是我第一次尝试进行后端图像下载,同时也进行了 swift,因为我来自 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
}
}
}
设置
uiimageview contentMode 是 scaleAspectFill
和 clipsToBounds 为真
请检查您的 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!)
})
}