Objective C downloads image method summary from remote url

  • 2020-05-09 19:22:03
  • OfStack

Objective C downloads images from remote url


- (UIImage *) getImageFromURL: (NSString *)theURL {
  UIImage *theImage = NULL;
  NSString *imageFileName = [BT_strings getFileNameFromURL:theURL];
  NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theURL]];
  theImage = [[UIImage alloc] initWithData:imageData];
  [BT_fileManager saveImageToFile:theImage fileName:imageFileName];
  return theImage;
}

objective C gets the image from the remote address and resizes it


NSString* imageURL = [NSString stringWithFormat: @"http://theimageurl.com/?id=%@", [[resultsEntries objectAtIndex:0] objectForKey: @"image_large"]];
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]]; 
UIImage* image = [[UIImage alloc] initWithData:imageData];
 
// resize image
CGSize newSize = CGSizeMake(100, 100);
UIGraphicsBeginImageContext( newSize );// a CGSize that has the size you want
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
 
//image is the original UIImage
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();    
 
imageHeight = image.size.height;
[imageMain setImage:newImage];
[imageData release];
[image release];

That's all for this article, I hope you enjoy it.


Related articles: