Avi Tsadok
Avi Tsadok

Reputation: 1843

UIImageView goes black inconsistently

I have a UITableView, contains some views in each cell, and one of the views is UIImageView. In the cellForRow method I am loading the UIImageView with an image, but sometimes, the UIImageView just shows a black screen.

I thought it can be for two reasons - Corrupted UIImage or Memory problem.

The UIImage is not corrupted (It's the same UIImage all the time), and I even checked what is the behavior when I put a manually corrupted image -> It goes white! not black...

I check if I have some memory warnings, ViewDidUnload launch -> nothing. Even simulated memory warning in the simulator, and nothing, it has nothing to do with that.

Does someone have any clue why it's happening?

Thanks!


Here is my cellForRow method. Basically, it's a list of cells with UIWebView and UIImageView on top, and when the table scrolls, I hide the webview and show the imageview, and vice versa. Maybe it has something to do with my problem, but I doubt it, cause the image is the same, and uiimagaview is not getting released.

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {   
   ImMailMessage *message = [_messages objectAtIndex:indexPath.row];

ImFullMessageTableViewCell *cell = (ImFullMessageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ImFullMessageTableViewCell"];

if (cell == nil)
{
    cell = [[[NSBundle mainBundle] loadNibNamed:@"ImFullMessageTableViewCell" owner:self options:nil] objectAtIndex:0];
    cell.delegate = self;

    //add gestures
    UIPinchGestureRecognizer *pinchGesture = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(cellPinched:)] autorelease];
    [cell addGestureRecognizer:pinchGesture];
    pinchGesture.delegate = self;

    UIPanGestureRecognizer *panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(cellPanned:)] autorelease];
    panGesture.minimumNumberOfTouches = 2;
    [cell addGestureRecognizer:panGesture];
    panGesture.delegate = self;

}


cell.threadState = _state;
cell.mailMessage = message;


UIImage *image = [self imageForMailMessage:message];
if (!image)
{
    // if no image, put the webview and reload it.
    cell.webView.mailMessage = message;     
    cell.webViewImageView.hidden = YES;
    cell.webViewImageView.image = nil;
    [cell.webView reloadMailMessage];

}
else // image exists!
{
    cell.webViewImageView.image = image;

    // replace the web view with the recived image.
    [cell replaceWebViewWithImageView];
}


return cell;
}

Upvotes: 1

Views: 1001

Answers (1)

David Hoerl
David Hoerl

Reputation: 41642

So in your "cellForRow", you first try to recycle old cells, then if none available you create a new one. You somehow get access to the UIImageView just after that (or you are using the one that the cell provides?)

The first thing I would do is in cellForRow don't even set an image - find the UIImageView, then either log it or assert() that its there, then set the background color to say green":

imageView.backgroundColor = [UIColor greenColor];

If you see a bunch of green colored rectangles where your image is, then you can proceed to the next step (after removing that green background!), and set the image - but LOG each time the image is set, that is log the image before you set it, so you can be 100% sure that in fact you have a valid image.

If you get this far, you got the green, your logging your images, but it still isn't working then add a comment and I'll check back.

EDIT: So continue NOT writing an image - you should have a imageView now just drawing its background.

Add a delegate method: "- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath "

Put a log there and for each cell, log its index, the imageView, the imageView.backgroundColor, and imageView.image (should be nil!). I suspect you will find that the backgroundcolor is not green, or there is some image

Upvotes: 1

Related Questions