multithreading - NSOperationQueue调用performSelectorOnMainThread导致应用程序崩溃

标签 multithreading memory crash nsthread

我正在使用以下代码从服务器下载一些图片,

-(void)viewDidLoad
{
     for (int i = 0 ; i < picsNames.count ; i++) {

         UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(i * 320.0, 0.0, 320.0, self.view.frame.size.height)];
         imageView.backgroundColor = [UIColor blackColor];
         imageView.tag = IMAGE_VIEWS_TAG + i;

         [scrollView addSubview:imageView];

         //getting the image
         NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getImage:) object:imageView];
         [queue addOperation:operation];
     }
}


-(void)getImage:(UIImageView *)imageView
{
     //getting the "image" from the server .....
     [self performSelectorOnMainThread:@selector(loadPic:) withObject:[NSArray arrayWithObjects:imageView, image,nil] waitUntilDone:YES];
}

-(void)loadPic:(NSArray *)imageAndImageViewArray
{
    //loading the image to imageview
    UIImageView *imageView = (UIImageView *)[imageAndImageViewArray objectAtIndex:0];
    imageView.image = (UIImage *)[imageAndImageViewArray objectAtIndex:1];
}

加载一些图片后,以下行会发出内存警告,并导致应用程序崩溃。
[self performSelectorOnMainThread:@selector(loadPic:) withObject:[NSArray arrayWithObjects:imageView, image,nil] waitUntilDone:YES];

我不知道如何解决这个问题。
感谢大家 :)

最佳答案

因此,在花了太多时间之后,我意识到错误是由于同时分配了太多UIImage对象而不是主NSTread的NSQueue引起的。

所以只是更改为代码,所以我当时只有10个UIImage对象。

关于multithreading - NSOperationQueue调用performSelectorOnMainThread导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080139/

相关文章:

c# - Task.StartNew 在运行时持有主线程

java线程通知

c - 我是 C 语言的新手,这种段错误的东西快要了我的命

C++将内存转换为数据结构

excel - #xslt #C++ Excel 在打印预览时突然崩溃

仅在 Google Pixel 6P 上发生 Android native 崩溃

c++ - 分离线程执行结束

java - 我的 Android/Java 线程需要信号量吗?

ios - iOS 上的高虚拟内存使用率 + 低分配

objective-c - 与 CALayer 相关的奇怪崩溃日志