[英]Loading image from URL on iPhone
I have no idea why this isn't working. 我不知道为什么这不起作用。
NSData *data = [NSData dataWithContentsOfURL:place.iconData];
UIImage *image = [UIImage imageWithData:data];
[imageView setImage:image];
imageView is an IBOutlet hooked up through IB. imageView是通过IB连接的IBOutlet。 place.iconData is http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png .
place.iconData是http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png 。 For some reason, though, it won't load.
但是由于某种原因,它不会加载。 Any help is much appreciated.
任何帮助深表感谢。
Try this: 尝试这个:
NSURL *url = [NSURL URLWithString: @"http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
[imageView setImage:image];
//or imageView.Image=image;
Is your data being freed before it got assigned? 在分配数据之前是否已将其释放?
Maybe try manually allocating memory for the NSData: 也许尝试为NSData手动分配内存:
NSData *data = [[NSData alloc] initWithContentsOfURL:place.iconData];
UIImage *image = [[UIImage alloc] initWithData:data];
[imageView setImage:image];
[data release];
[image release];
You need to use NSURL. 您需要使用NSURL。 Follow this url http://iosdevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html
请遵循以下网址http://iosdevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.