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.youkuaiyun.com/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的打印顺序每次都 不太一样,这个可能跟系统执行有关.
子线程在后台运行: performSelectorInBackground
最新推荐文章于 2024-11-27 09:50:00 发布
本文介绍了一个iOS应用中使用子线程加载网络图片到UIImageView的具体实现过程,并讨论了NSAutoreleasePool的正确使用方式以避免内存泄漏。
5795

被折叠的 条评论
为什么被折叠?



