Replacing a UIImageView inside of an NSMutableArray - weird redrawing issue

Discussion in 'iOS Programming' started by Legolover64, May 2, 2010.

  1. Legolover64 macrumors regular

    Joined:
    Feb 24, 2008
    #1
    I have an NSMutableArray of UIImageViews that have been added as subviews of a particular view.

    When I replace a view like this:

    Code:
        UIImageView *tempview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
        tempview.frame = CGRectMake(0, 0, 667, 920);
        [drawViewController.drawImage replaceObjectAtIndex:0 withObject:tempview];
    I can see that the view is replaced, but doesn't redraw. Occasionally, this will cause the application to become unresponsive. I've tried stepping through the functions, but that doesn't seem to help. I tried `setNeedsDisplay` but that doesn't seem to work either. Is there something else I'm missing?
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    I'm a little confused. You've added the image to an NSMutableArray I'm guessing, but you haven't added it to any view or contentView.
     
  3. Legolover64 thread starter macrumors regular

    Joined:
    Feb 24, 2008
    #3
    xStep,

    I guess I'm a little confused about what exactly is "inside" of my NSMutableArray.

    Here's how the array gets initialized:

    Code:
    UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    UIImageView *l2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"transparent.png"]];
    UIImageView *l3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"transparent.png"]];
    UIImageView *l4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"transparent.png"]];
    drawImage = [NSMutableArray arrayWithObjects:bg, l2, l3, l4, nil];
    for(int i = 0; i < 4; i++)
    {
    	[self.view addSubview:[drawImage objectAtIndex:i]];
    }
    Am I right in thinking that any change to the drawImage array also effects self.view? Or no? If so, how would I do things like switching out UIImageViews in both the array and the UIView?
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Changing the contents of the array will not update the view that's visible. Why don't you change the image in the image view rather than make a new view?

    Code:
    imageview.image = myUIImage;
     
  5. Legolover64 thread starter macrumors regular

    Joined:
    Feb 24, 2008
    #5
    Sadly, I can't seem to actually change the aspects of an object within an NSMutableArray for whatever reason, which I think lead me to my original concern. For example, this line doesn't compile:

    Code:
    [[drawViewController drawImage]objectAtIndex:0].image = [UIImage imageNamed:@"background.png"];
    Am I using the NSMutableArray wrong?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    No, it's more like you are using Objective-C wrong. objectAtIndex: is going to return an id. id has no idea that it is a UIImageView. Therefore, it doesn't understand the image property you are trying to access.
     
  7. kaydell.leavitt macrumors regular

    Joined:
    Apr 19, 2010
    #7
    Type-casting

    You can typecast an id to be a UIImage view:

    Code:
    UIImageView imageView = (UIImageView *)id;
    
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Yes, of course. I just didn't come out and say that directly because I was hoping the OP would come to that discovery on their own. I believe it is more satisfying (and more memorable) if one gets their own eureka moments rather than just being told the answer. Hints > copy&paste code.
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    I'm really not big fan of this kind of nested code. I would probably write this in two or three lines but you can do it like this:

    Code:
    [[[drawViewController drawImage]objectAtIndex:0]setImage:[UIImage imageNamed:@"background.png"]];
    without a typecast or a compiler error/warning. The compiler doesn't know everything it probably should when it comes to properties.
     

Share This Page