0

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

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

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

4

1 回答 1

0

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

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

于 2013-03-12T23:58:02.063 回答