Posted By

suibhne on 11/12/09


Tagged

resize image Objective-c iphone


Versions (?)

Who likes this?

12 people have marked this snippet as a favorite

alladinian
teedy
trawor
stationstops
avanadra
bobbym245
sb
nsbuckner
davelb20
synced
rvachere
zhendalf


Load Image from URL and then Resize


 / Published in: Objective C
 

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

  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];
  4.  
  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)];
  9.  
  10. //image is the original UIImage
  11. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  12. UIGraphicsEndImageContext();
  13.  
  14. imageHeight = image.size.height;
  15. [imageMain setImage:newImage];
  16. [imageData release];
  17. [image release];

Report this snippet  

You need to login to post a comment.