0

I have a UIImage that i know its ok before scaling. I need to scale it so i can set it to ccsprite . I scale it ,but then i got a white image ..

UIImage *scaled=[newImage copy];
//scale the image
CGRect screenRect = CGRectMake(0, 0, 640.0, 960.0);
UIGraphicsBeginImageContext(screenRect.size);
scaled = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CCSprite * image = [CCSprite spriteWithCGImage:scaled.CGImage 
                                           key:[NSString stringWithFormat:@"%d",[localImagesSpritesArray count]]];

What could it be ?

Sergey Kuryanov
  • 6,114
  • 30
  • 52
Curnelious
  • 1
  • 16
  • 76
  • 150
  • Where are you drawing the image to be scaled into the new context? **hint**: `CGContextDrawImage(context, screenRect, scale.CGImage);` – Till Mar 16 '13 at 20:50
  • 1
    Why do you make a copy of `newImage` and then throw away the result? – rmaddy Mar 16 '13 at 21:05
  • Also, you should not hard-code the `screenRect`. You should get it from UIScreen. You may also want to use `UIGraphicsBeginImageContextWithOptions` instead of `UIGraphicsBeginImageContext`; the `WithOptions` version lets you specify the scale factor of the context, or ask it to auto-detect. The function you're using unconditionally uses a scale factor of `1.0`, which is for non-Retina images. – Peter Hosey Mar 17 '13 at 08:55

1 Answers1

1
UIGraphicsBeginImageContext(screenRect.size);
[newImage drawInRect:screenRect]; //you have to add this line 
scaled = UIGraphicsGetImageFromCurrentImageContext();
Nick C
  • 645
  • 3
  • 6
  • thanks, but it somehow change the image ratio and stretch it.. whats the relations between the line you have added and the screenRect resolutions ? they should be the same ? – Curnelious Mar 16 '13 at 22:41
  • @Rant: You told it to draw in that rectangle, so it will scale to that rectangle's size. If it has to stretch to achieve that size, it'll stretch. If you want to do an aspect-fit, then see this question, from over in the “Related” list on the right side: http://stackoverflow.com/q/1703100/30461 – Peter Hosey Mar 17 '13 at 08:52