Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing an image with stretchableImageWithLeftCapWidth

I'm trying to resize an image using stretchableImageWithLeftCapWidth: it works on the simulator, but on the device, vertical green bars appears.

I've tried to use imageNamed, imageWithContentOfFile and imageWithData lo load the image, it doesnt change.

UIImage *bottomImage = [[UIImage imageWithData:
     [NSData dataWithContentsOfFile:
    [NSString stringWithFormat:@"%@/bottom_part.png", 
     [[NSBundle mainBundle] resourcePath]]]] 
       stretchableImageWithLeftCapWidth:27 topCapHeight:9];

UIImageView *bottomView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 200+73, 100, 73)];
[self.view addSubview:bottomView];
UIGraphicsBeginImageContext(CGSizeMake(100, 73));
[bottomImage drawInRect:CGRectMake(0, 0, 100, 73)];
UIImage *bottomResizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
bottomView.image = bottomResizedImage;

See the result: the green bars shouldn't be there, they dont appear on the simulator.

alt text http://www.quicksnapper.com/files/5161/96232162149F1C14751048_m.png

like image 585
Stephan Burlot Avatar asked Apr 24 '09 14:04

Stephan Burlot


2 Answers

Did you figure this out?

Seems like it might be a bug with UIGraphicsGetImageFromCurrentImageContext. If I draw an image that has transparency I get red/green artifacts. These only appear on the device (not in the sim). Also, if I remove transparency from the image, the artifacts go away.

Update: Some more weirdness. I was using PNGs before, so I tried using a transparent gif instead. Using a GIF, shows the artifact problem on the sim.

Victory! Found a solution:

Turn off 'Compress PNG Files' in the build settings for your project. Disabling this makes the PNG transparency work without any artifacts.

like image 177
Nate Weiner Avatar answered Nov 07 '22 13:11

Nate Weiner


It seems like you're writing a lot of unnecessary code, but perhaps that's just because it's out of context and there's more to it that I'm missing.

To get the image:

[[UIImage imageName: @"bottom_part.png"] stretchableImageWithLeftCapWidth: 27 topCapHeight: 9];

What part of your code are you displaying this image? i.e. in what method call are you drawing the images above?

Why not just use a UIImageView and put the image in there? Then you don't need to do any of the image context drawing etc.

like image 21
Chris Garrett Avatar answered Nov 07 '22 12:11

Chris Garrett