您的位置:首页 > 移动开发

This application is modifying the autolayout engine from a background thread, which can lead to engi

2016-08-19 15:49 423 查看
当添加了子线程,没有回到主线程中刷新UI 会报错

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.

此应用程序从一个后台线程修改自动布局引擎,这可能导致引擎腐败和奇怪的崩溃。这将导致将来的版本的一个例外。

解决方法:

回调主线程中刷新UI

[self performSelectorOnMainThread:@selector(updateImage:) withObject:data waitUntilDone:YES];


/**
*  下载图片1
*
*  @param sender
*/
- (IBAction)downloadImageOne:(id)sender {

// 方法一 使用对象方法
// 创建一个线程,第一个参数是请求的操作, 第二个参数是操作方法的参数
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImage) object:nil];
// 启动一个线程,注意启动一个线程并非就一定立即执行,而是处于就绪状态,当系统调度时才真正执行
[thread start];

/**
*  循环次数多一点 模拟单线程卡顿现象 运行那么多次 要三分钟
*/
//    for (int i = 0; i < 100000; i++) {
//        NSLog(@"%d", i);
//    }

// 上面循环结束后才能下载图片
//    self.iconOneimageView.image = [self getImage:[self requestDataUrl:@"http://b.hiphotos.baidu.com/image/h%3D200/sign=9b711189efc4b7452b94b016fffd1e78/3c6d55fbb2fb4316fc06edda24a4462309f7d371.jpg"]];
}


/**
*  加载图片
*/
- (void)loadImage {
// 放在线程中就不会出现卡死的现象
//    for (int i = 0; i < 100000; i++) {
//        NSLog(@"%d", i);
//    }

// 请求数据
NSData *data = [self requestDataUrl:@"http://b.hiphotos.baidu.com/image/h%3D200/sign=9b711189efc4b7452b94b016fffd1e78/3c6d55fbb2fb4316fc06edda24a4462309f7d371.jpg"];

// 不在主线程中刷新UI 会报错
// 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.
//    [self updateImage:data];

// 将数据显示到UI控件,注意只能在主线程中更新UI
// 另外performSelectorOnMainThread方法是NSObject的分类方法,每个NSObject对象都有此方法
// 它调用的selector方法是当前调用控件的方法,例如使用UIImageView调用的selector就是UIImageView的方法
// object 代表调用方法的参数,不过只能传递一个参数(如果有多个参数使用对象进行封装)
// waitUntiDone 是否线程任务完成执行
[self performSelectorOnMainThread:@selector(updateImage:) withObject:data waitUntilDone:YES];

}


/**
*  将图片显示到界面
*
*  @param imageData <#imageData description#>
*/
- (void)updateImage:(NSData *)imageData {

self.iconOneimageView.image = [self getImage:imageData];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  主线程刷新UI
相关文章推荐