UI 从进入主队列后变得无响应
UI becomes non responding after getting from in to the main queue
我正在尝试从 url 延迟加载图像并更新 UI 一旦图像仅在主队列中接收到,UI 正在更新但是视图变得无响应,我正在使用 GCD 创建一个新线程以从 URL
获取数据
let imageUrl : NSURL = NSURL(string: SiteAPIURL + (DEFAULTS.valueForKey(kInviterImageUrl) as!String))!
let request: NSURLRequest = NSURLRequest(URL: imageUrl)
SwiftLoader.show(title: "Loading", animated: true)
let queue : dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
dispatch_async(queue) { () -> Void in
let data : NSData = NSData(contentsOfURL: imageUrl)!
let image = UIImage(data: data)!
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.userImage.image = image
self.userImageWide.image = image
})
}
}
"DISPATCH_QUEUE_PRIORITY_HIGH: Items dispatched to the queue run at high priority; the queue is scheduled for execution before any default priority or low priority queue."
那是文档中的内容,因此请使用优先级较低的其他内容,因为这可能会阻塞默认队列,例如 QOS_CLASS_USER_INITIATED。
这就是你想要的
if let url = imageUrl{
let qos = Int(QOS_CLASS_USER_INITIATED.rawValue)
dispatch_async(dispatch_get_global_queue(qos, 0), {
let data = NSData(contentsOfURL: imageUrl)
dispatch_async(dispatch_get_main_queue(), {
if data != nil {
if let image = UIImage(data: data){
self.userImage.image = image
self.image = UIImage(data: imageData!)
self.userImageWide.image = image
}
}
})
})
}
我正在尝试从 url 延迟加载图像并更新 UI 一旦图像仅在主队列中接收到,UI 正在更新但是视图变得无响应,我正在使用 GCD 创建一个新线程以从 URL
获取数据let imageUrl : NSURL = NSURL(string: SiteAPIURL + (DEFAULTS.valueForKey(kInviterImageUrl) as!String))!
let request: NSURLRequest = NSURLRequest(URL: imageUrl)
SwiftLoader.show(title: "Loading", animated: true)
let queue : dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
dispatch_async(queue) { () -> Void in
let data : NSData = NSData(contentsOfURL: imageUrl)!
let image = UIImage(data: data)!
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.userImage.image = image
self.userImageWide.image = image
})
}
}
"DISPATCH_QUEUE_PRIORITY_HIGH: Items dispatched to the queue run at high priority; the queue is scheduled for execution before any default priority or low priority queue." 那是文档中的内容,因此请使用优先级较低的其他内容,因为这可能会阻塞默认队列,例如 QOS_CLASS_USER_INITIATED。 这就是你想要的
if let url = imageUrl{
let qos = Int(QOS_CLASS_USER_INITIATED.rawValue)
dispatch_async(dispatch_get_global_queue(qos, 0), {
let data = NSData(contentsOfURL: imageUrl)
dispatch_async(dispatch_get_main_queue(), {
if data != nil {
if let image = UIImage(data: data){
self.userImage.image = image
self.image = UIImage(data: imageData!)
self.userImageWide.image = image
}
}
})
})
}