How can I display an image from a url?

I have a tmpImgURLStr string variable that contains a URL like www.abc.com/img.png . I want to display this image in my image. For this, I use some kind of code, but it does not work, which is given below:

 NSLog(@"Img URL === %@",tmpImgURLStr); NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",tmpImgURLStr]]]; UIImage *myimage = [[UIImage alloc] initWithData:mydata]; [logoImg setImage:myimage]; 
+4
source share
3 answers

As far as I can tell by your url - you have a pdf, not an image. Typically, WebView are used to display such data.

Update

Your start of NSData too long. You can initiate a URL without providing a formatted string:

 NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:tmpImgURLStr]]; 

I also noticed that your url has no protocol. You can try adding http:// or https:// , and then see what happens. And just in case, check if your logoImg is logoImg connected to the NSImageView in your NIB.

+3
source

try it

 NSURL *imageurl = [NSURL URLWithString:@"http://www.chakrainteractive.com/mob/ImageUpoad/pic2-2.png"]; NSData *imagedata = [[NSData alloc]initWithContentsOfURL:imageurl]; UIImage *image = [UIImage imageWithData: imagedata]; [logoImg setImage: image]; 
+3
source

Try this code instead of yours. Maybe it will work.

 logoImg=[[IUImageView alloc]initWithFrame:CGRectMake(10,10,300,460)]; NSLog(@"Img URL === %@",tmpImgURLStr); NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:tmpImgURLStr]]]; UIImage *myimage = [[UIImage alloc] initWithData:mydata]; [logoImg setImage:myimage]; 

-Voice coding ....

0
source

Source: https://habr.com/ru/post/1313824/


All Articles