0

I'm using the following code to download some pic from the server,

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

After loading some pictures the following line gives memory warning and crashes the app.

[self performSelectorOnMainThread:@selector(loadPic:) withObject:[NSArray arrayWithObjects:imageView, image,nil] waitUntilDone:YES];

I don't know how to solve this issue. Thanks everyone :)

Mona
  • 5,939
  • 3
  • 28
  • 33

1 Answers1

0

So after spending too much time on this I realized that the error is due to having too many UIImage objects alloc at the same time no the NSQueue of main NSTread.

So Just changed to code so I have only 10 UIImage objects at the time.

Mona
  • 5,939
  • 3
  • 28
  • 33