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.

时间:2023-03-09 05:21:44
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.

一,经历

<1> 使用SDWebImage下载 成功图片后,将图片设置给 self.imageView.image,提示如题所示的错误提示.

<2>第一反应就是慢慢注释掉代码进行调试,结果发现是在成功的回调中出事了,代码如下:

 - (void)setupDownloadImage{
NSURL *url = [NSURL URLWithString:@"http://i4.pdim.gs/dmfd/200_200_100/t01f117f76fc58c257c.gif"]; SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:url options: progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (data != nil && image != nil) {
UIImage *image = [UIImage sd_animatedGIFWithData:data];
self.imageView.image = image;
}else {
UIImage *image = [UIImage imageNamed:@""];
self.imageView.image = image;
}
}];
}

<3>初步怀疑是sdwebimage 设置gift 图片失败,就在成功的回调中使用了一张本地的图片,依旧报错.

<4>仔细研究了提示,发现了关键词"a background thread",才知道是 sdwebimage 的后台线程在执行一些操作,仔细想了想才知道是更新 UI的操作必须在主线程中执行.

二,总结

<1>一定要注意子线程中对更新 UI 的使用.