iOS Dev: How to view image from url and resize it?

Images from webservices or servers are not always available in the appropriate size. I figured out a way to resize images without much loss of quality and how to view them by using the orginial url.

CGSize size = CGSizeMake(100, 100);
NSString *imageUrl = [@"http://www.myUrl.com/" stringByAppendingString:@"myImage.png"];
NSData *receivedData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]] retain];
preViewImage.image = [utils resizeImage:[[UIImage alloc] initWithData:receivedData] reSize:size];

And now resize the image to a given size.

- (UIImage*)resizeImage:(UIImage*)aImage reSize:(CGSize)newSize;
{
      UIGraphicsBeginImageContext(newSize);
      [aImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
      UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return newImage;
}

Leave a comment