how can i display image from url?

☆樱花仙子☆ 提交于 2019-12-23 15:50:35

问题


I have a string variable tmpImgURLStr which contains URL like www.abc.com/img.png. i want to display that image in my imageView for that i have use some code but it's not working 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];

回答1:


As far as I can tell from your url - you have pdf, not an image. Usually WebViews are used for displaying this sort of data.

Update

Your NSData initiation is kinda too long. You can actually initiate a URL without supplying formatted string:

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

Also I've noticed that your URL is without protocol. You may want to try adding http:// or https:// to it and then see what happens. And just in case check if your logoImg is actually wired to the NSImageView in your NIB.




回答2:


Try this

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:


Try this code instead of yours .. May be 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];

-Happy coding....



来源:https://stackoverflow.com/questions/3115643/how-can-i-display-image-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!