問題描述:
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
問題代碼 :
func loadImage(imageURL:String){
print("image url->",imageURL)
let url = NSURL(string: imageURL)
let data = try? NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingMappedIfSafe)
let image = UIImage(data: data!)
self.images.append(image)
}
因為執(zhí)行
NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingMappedIfSafe)
會阻塞主進程,因此我們需要自行異步調(diào)用此方法來解決問題
改成下面的樣子后就好了
因為是一組圖片,所以我們增加一個異步加載隊列組來管理加載
func loadImages(images:[String]) {
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
let queueGroup = dispatch_group_create()
dispatch_group_async(queueGroup, queue, {
for imageURL in images{
self.loadImage(imageURL)
}
})
dispatch_group_notify(queueGroup, dispatch_get_main_queue(), {
//將加載完成后的圖片添加到視圖
self.addImages()
})
}
func loadImage(imageURL:String){
print("image url->",imageURL)
let url = NSURL(string: imageURL)
let data = try? NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingMappedIfSafe)
let image = UIImage(data: data!)
self.images.append(image!)
}
更具體的線程講解可以參考這篇文章或者自行搜索
http://blog.csdn.net/totogo2010/article/details/8016129