performSelectorInBackground

NSLog( @"main thread begin..." );


[self performSelectorInBackground: @selector(getImages) withObject: nil];


NSLog( @"main thread end..." );

- (void)getImages
{

NSLog( @"one thread begin..." );

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *str_url = @"http://avatar.csdn.net/2/5/4/3_iorchid.jpg";

NSURL *url = [NSURL URLWithString: str_url];
NSData *data = [NSData dataWithContentsOfURL: url];

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: data]];
imageView.frame = CGRectMake(50, 200, 80, 40);

imageView.layer.cornerRadius = 6;
imageView.layer.masksToBounds = YES;
[self.window addSubview: imageView];
NSLog( @"one thread end..." );

[pool release];

}
刚开始写getImages函数的时候,提示autorelease pool …leak之类的错误,后来发现,其实子进程默认不建立autoreleasepool,还得手动建立才行.
log的打印顺序每次都 不太一样,这个可能跟系统执行有关.

原文地址:https://www.cnblogs.com/linyawen/p/2576190.html