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

 1 - (void)setupDownloadImage{
 2     NSURL *url = [NSURL URLWithString:@"http://i4.pdim.gs/dmfd/200_200_100/t01f117f76fc58c257c.gif"];
 3 
 4     SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
 5    [downloader downloadImageWithURL:url options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
 6         if (data != nil && image != nil) {
 7             UIImage *image = [UIImage sd_animatedGIFWithData:data];
 8                 self.imageView.image = image;
 9         }else {
10             UIImage *image = [UIImage imageNamed:@"12345"];
11             self.imageView.image = image;
12         }
13     }];
14 }

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

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

二,总结

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

原文地址:https://www.cnblogs.com/lz465350/p/5196478.html