2013-02-26 26 views
0

我使用下面的代码从服务器下载一些PIC,NSOperationQueue调用performSelectorOnMainThread的应用程序崩溃

-(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]; 

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

回答

0

因此,花了太多时间后,我意识到,错误是由于有太多的UIImage对象分配在同一时间没有主NSTread NSQueue。

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