Load Image from URL and then Resize

/ Published in: Objective C
Save to your folder(s)

Some code that will load an image from a remote URL, and then resize it.

Copy this code and paste it in your HTML
  1. NSString* imageURL = [NSString stringWithFormat: @"http://theimageurl.com/?id=%@", [[resultsEntries objectAtIndex:0] objectForKey: @"image_large"]];
  2. NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]];
  3. UIImage* image = [[UIImage alloc] initWithData:imageData];
  5. // resize image
  6. CGSize newSize = CGSizeMake(100, 100);
  7. UIGraphicsBeginImageContext( newSize );// a CGSize that has the size you want
  8. [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  10. //image is the original UIImage
  11. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  12. UIGraphicsEndImageContext();
  14. imageHeight = image.size.height;
  15. [imageMain setImage:newImage];
  16. [imageData release];
  17. [image release];

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.