图片从 XML 到 Cell
Image from XML to Cell
我想将 ASYNC
图像加载到单元格。
我的代码是:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as xmlParserDatenCell
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
var test = UIImage(data: NSData(contentsOfURL: NSURL(string: daten[imagePath.row]["bildlink"]!)!)!)
cell.bild1.image = test
})
我的答案是nil
。
不知道。 :(
我在网上搜索了几个小时。
编辑:
var daten = [String:String] 看起来是我的约会对象。
我的解析器 returns 变成了:
daten.append(字符串:字符串)
要发出异步网络请求,您可以使用:
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue(), completionHandler:{
(response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
// do stuff with response, data & error here
})
如果您在 cellForRowAtIndexPath: 内部使用它,您最终可能不会得到与预期相同的单元格,因为它是异步的并且单元格会被重复使用,因此请确保在设置图像之前拥有正确的单元格:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView!.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
var urlRequest = NSMutableURLRequest(URL: NSURL(string: daten[indexPath.row]["bildlink"]!)!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue(), completionHandler:{
(response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if let cellToUpdate = self.tableView?.cellForRowAtIndexPath(indexPath) {
cellToUpdate.imageView?.image = UIImage(data: data)
// need to reload the view, which won't happen otherwise since this is in an async call
cellToUpdate.setNeedsLayout()
}
})
}
这里有很多地方出了问题。当您离开主队列进行异步时,您必须 return 在设置图像之前进入主队列。此外,您需要调试代码以确保在每一步都得到您期望的结果(即 daten[0]["bildlink"]
实际上是 return 图片地址吗?)
无论如何,如果您使用 swift 1.2 并且 xml 实际上是 return 正在处理一些数据,那么这里有一些代码应该可以工作。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in // background
var loadedImage: UIImage? //optional image, will be set if the rest succeeds
if let link = daten[indexPath.row]["bildlink"], // address for image
url = NSURL(string: link), // .. to URL
data = NSData(contentsOfURL: url), // .. to data
image = UIImage(data: data) { // .. image from data
loadedImage = image
}
dispatch_async(dispatch_get_main_queue()) { () -> Void in // return to main thread
cell.bild1.image = loadedImage ?? nil // if above successful, actually set image
}
}
在设置图像之前,您还应该检查以确保单元格未被重复使用。你可以用很多方法来做到这一点,但我会添加一个 属性 就像 imageAddress
来保存来自 daten[0]["bildlink"]
的字符串并像 if cell.imageAddress == link { cell.bild1.image = loadedImage }
一样引用它
我想将 ASYNC
图像加载到单元格。
我的代码是:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as xmlParserDatenCell
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
var test = UIImage(data: NSData(contentsOfURL: NSURL(string: daten[imagePath.row]["bildlink"]!)!)!)
cell.bild1.image = test
})
我的答案是nil
。
不知道。 :(
我在网上搜索了几个小时。
编辑:
var daten = [String:String] 看起来是我的约会对象。 我的解析器 returns 变成了:
daten.append(字符串:字符串)
要发出异步网络请求,您可以使用:
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue(), completionHandler:{
(response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
// do stuff with response, data & error here
})
如果您在 cellForRowAtIndexPath: 内部使用它,您最终可能不会得到与预期相同的单元格,因为它是异步的并且单元格会被重复使用,因此请确保在设置图像之前拥有正确的单元格:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView!.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
var urlRequest = NSMutableURLRequest(URL: NSURL(string: daten[indexPath.row]["bildlink"]!)!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue(), completionHandler:{
(response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if let cellToUpdate = self.tableView?.cellForRowAtIndexPath(indexPath) {
cellToUpdate.imageView?.image = UIImage(data: data)
// need to reload the view, which won't happen otherwise since this is in an async call
cellToUpdate.setNeedsLayout()
}
})
}
这里有很多地方出了问题。当您离开主队列进行异步时,您必须 return 在设置图像之前进入主队列。此外,您需要调试代码以确保在每一步都得到您期望的结果(即 daten[0]["bildlink"]
实际上是 return 图片地址吗?)
无论如何,如果您使用 swift 1.2 并且 xml 实际上是 return 正在处理一些数据,那么这里有一些代码应该可以工作。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in // background
var loadedImage: UIImage? //optional image, will be set if the rest succeeds
if let link = daten[indexPath.row]["bildlink"], // address for image
url = NSURL(string: link), // .. to URL
data = NSData(contentsOfURL: url), // .. to data
image = UIImage(data: data) { // .. image from data
loadedImage = image
}
dispatch_async(dispatch_get_main_queue()) { () -> Void in // return to main thread
cell.bild1.image = loadedImage ?? nil // if above successful, actually set image
}
}
在设置图像之前,您还应该检查以确保单元格未被重复使用。你可以用很多方法来做到这一点,但我会添加一个 属性 就像 imageAddress
来保存来自 daten[0]["bildlink"]
的字符串并像 if cell.imageAddress == link { cell.bild1.image = loadedImage }