NSThread 在主线操作的三个方法

时间:2023-03-09 05:30:05
NSThread 在主线操作的三个方法
- (void)createNSThread444{

    UIImage *image = [UIImage imageNamed:@"图片名字"];

    /**
1
performSelectorOnMainThread :在主线程操作
参数1:(nonnull SEL)方法名字
参数2:(nullable id) self 执行这个方法的对象
参数3:(BOOL)方法名字 ,YES 代表执行到这一行 一定要执行完任务才会执行下面的数据,NO 代表不管当前任务是否执行完,都会执行下面的代码
*/
[NSThread performSelectorOnMainThread:@selector(createNSThread111) withObject:image waitUntilDone:NO]; /**
2
performSelectorOnMainThread 是 NSObject的拓展,所以只要是继承自NSObject的对象都可以使用这个方法
setImage: 是imageView自带的方法,
*/
[self.iv performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO]; /**
3
onThread 指定在那个线程
*/
[self performSelector:@selector(createNSThread111) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO]; NSLog(@"执行111");
NSLog(@"执行222");
NSLog(@"执行333");
NSLog(@"执行444");
} -(void)showImage:(UIImage *)image{
self.iv.image = image;
}