我的代码有问题。我试图通过StringBuilder
下载图片,然后将其设置为UIImage我似乎遇到了问题,我希望有人能看到我做错了什么。
塞图伊:
uiMovieTitle.text = self.movies![movieIndex].title
var finalImageUrl = StringBuilder()
let session = URLSession(configuration: .default)
let downloadPicTask = session.dataTask(with: finalImageUrl) { (data, response, error) in
// The download has finished.
if let e = error {
print("Error downloading cat picture: \(e)")
} else {
// No errors found.
// It would be weird if we didn't have a response, so check for that too.
if let res = response as? HTTPURLResponse {
print("Downloaded cat picture with response code \(res.statusCode)")
if let imageData = data {
// Finally convert that Data into an image and do what you wish with it.
let image = UIImage(data: imageData)
// Do something with your image.
uiMoviePoster.image = image
} else {
print("Couldn't get image: Image is nil")
}
} else {
print("Couldn't get response code for some reason")
}
}
}
downloadPicTask.resume()
}
字符串拼接
func StringBuilder() -> (String){
let posterBase = "http://image.tmdb.org/t/p/w1920"
let linkEnd = self.movies?[movieIndex].posterPath
var finalLink = ""
finalLink = posterBase + linkEnd!
return finalLink
}
我也有另一个下载,它为我提供了一个电影列表(JSON),对
StringBuilder
非常重要。 最佳答案
编译器之所以抱怨是因为函数SwiftBuilder
返回一个String
,并且URLSession
上有多个名为dataTask(with:completion:)
的方法,但是没有一个方法将String
作为第一个参数。
如果需要SwiftBuilder
继续返回代码其他部分的字符串,那么在这里需要将该字符串转换为URL。
类似于以下的方法应该有效:
let session = URLSession(configuration: .default)
let imageUrlString = StringBuilder()
if let imageUrl = URL(string: imageUrlString) {
let downloadPicTask = session.dataTask(with: imageUrl) { (data, response, error) in
// The download has finished.
等等。。。如果有道理的话告诉我。
关于ios - 对成员'datatask(with:completionhandler :)'的不明确引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41780591/