1

I tried to resize image to fit the frame with the code below but the image is kind of distorted.

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
     UIGraphicsBeginImageContext( newSize );
     [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();

     return newImage;
}


    UIImage* image =[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    deal.Image=[tfbImageHelper imageWithImage:image scaledToSize:newSize];
user1302602
  • 419
  • 2
  • 6
  • 16

1 Answers1

1

Yes, because you should use UIGraphicsBeginImageContextWithOptions and the last argument should be 0.0f (which means, to keep the current scale). It's distorted, because you're testing it on a retina display and the scale factor there is 2. Also make sure, you are keeping the aspect ratio, when assigning the new size.

graver
  • 15,183
  • 4
  • 46
  • 62
  • How do you keep the aspect ratio? – user1302602 Aug 17 '12 at 22:23
  • I tried it and still not perfect. It is scaling from larger image to smaller image. – user1302602 Aug 17 '12 at 22:23
  • 1
    I was going to answer with the scale. If you have an image sized 200 wide and 100 tall, and you resize to 50, 50, its going to be distorted. You need to find a solution that has you scale both dimensions the same - so you would say mean that newsize is the MAX size, and find the scaling that makes the image fit perfectly in one dimension. – David H Aug 17 '12 at 23:30