Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

OBJECTIVE - C (XCode) How to extract an image from a url and place it in an imag

ID: 3581525 • Letter: O

Question

OBJECTIVE - C (XCode)

How to extract an image from a url and place it in an image view? I am trying to extract an image from a website and place it in an image view. The code does not work:

NSURL *URL = [NSURL URLWithString:[self.url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

NSData *data = [NSData dataWithContentsOfURL:URL];

UIImage *img = [[UIImage alloc] initWithData:data];

UIImageView *image = [[UIImageView alloc] initWithImage:img];

I can load a web view, but I can't seem to extract an image.

Explanation / Answer

by simply replacing ur code with the following statements

NSString *ImageURL = @"YourURLHere";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
imageView.image = [UIImage imageWithData:imageData];

or

you can write like this

NSURL *url = [NSURL URLWithString:@"http://url_goes_here.com/logo.png"];
imageView.image = [UIImage imageWithCIImage:[CIImage imageWithContentsOfURL:url]];

Thank you