Objective C: Image download method summary from remote url
This article will share with you two ways to use Objective C to download images from a remote url. They are all used in personal projects. We recommend them to you in summary. If you need them, you can refer to them.
Objective C downloads images from remote URLs
?
1 2 3 4 5 6 7 8 |
-(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 an image from a remote address and modifies its size.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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]; |
The above is all the content of this article. I hope you will like it.