2

I have a UIImageView with an image that I want to be stretchable. In order to achieve that I do the following in viewDidLoad:

-(void) buildResizableDescriptionAreaBackground
{
    UIImage *image = [UIImage imageNamed:@"description_area_background"];
    CGFloat top = ceilf(image.size.height / 2.0) - 1;
    CGFloat bottom = floorf(image.size.height / 2.0);
    CGFloat left = ceilf(image.size.height / 2.0) - 1;
    CGFloat right = floorf(image.size.height / 2.0);
    if ([image respondsToSelector:@selector(resizableImageWithCapInsets:)])
    {
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)];
    }
    else 
    {
        image = [image stretchableImageWithLeftCapWidth:left topCapHeight:top];
    }
    [self.descriptionAreaBackgroundImage setImage:image];
    [self.descriptionAreaBackgroundImage setHighlightedImage:image];
}

When I debug this I make sure that the descriptionAreaBackgroundImage is not nil, not hidden, alpha = 1 and with the right frame. Also I make sure that the image is not nil, have the right size and that the resizable image is also not nil. And yet the image is not set to the UIImageView.

If I do this at viewDidAppear: everything works fine. If I set a regular image (Not stretchable, even at viewDidLoad) everything works fine.

As far as I know any setup that does not require superview or window can be done at viewDidLoad, so why isn't it working?

Avi Shukron
  • 6,088
  • 8
  • 50
  • 84

2 Answers2

1

It looks like your UIEdgeInsetsMake() call is specifying an area of 0x0 points. Try making sure you specify a 1x1 area.

For example, your stretchable image has a size of 7x7 pixels and you want the centre pixel to stretch, you would specify edge insets like so: UIEdgeInsetsMake(3.0f, 3.0f, 3.0f, 3.0f)

Andy Barnard
  • 696
  • 1
  • 5
  • 15
  • Interesting theory, but testing proved it wrong. The code in the question updated to reflect the testing that I did to ensure 1x1 stretchable area in the middle. Another thing is that even with the old code, if I did it at viewDidAppear: it worked just fine (with nice stretching, no weird effects whatsoever) – Avi Shukron Aug 20 '12 at 06:47
  • Hmm shame. You need to find what it is in viewWillAppear that is different. Could it be that you have set up some other view in viewDidLoad that has caused the imageview to resize itself to nothing (UIImageView has its auto resizing property to all by default). Or perhaps you have the imageview outside the bounds of its superview with clipsToBounds turned on. Just suggestions as its impossible to debug from here obviously. – Andy Barnard Aug 20 '12 at 07:48
  • I found the problem. Nothing fancy, just good old IBOutlet mistake...Thanks for the help anyway! – Avi Shukron Aug 20 '12 at 07:56
-1

OK, I found what causing it, stupid mistake.

In my viewDidLoad, I'm loading another .xib file containing additional views:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Some setups
    [[NSBundle mainBundle] loadNibNamed:@"RatingDialog" owner:self options:nil];
    // More setups...
    [self buildResizableDescriptionAreaBackground]; // NOT WORKING!!
}

Apparently, one of the UIImageViews in that nib was also connected to my descriptionAreaBackground outlet by mistake. So when I set the resizable image, it was to the wrong view...

How dumb.

Avi Shukron
  • 6,088
  • 8
  • 50
  • 84