我一直在尝试将矩形按钮的背景图像设置为我的网络服务器上的 .png 文件。我已经在视图控制器 .h 中声明了该按钮,并将其添加到 .m viewdidload 下:
[mybutton setImage: [UIImage imageNamed: @"http://www.myserver.com/index_files/stacks_image_14.png"] forState: UIControlStateHighlighted];
但按钮中没有显示任何内容。在尝试将图像分配给对象之前,我一直在阅读 NSSURL 以了解有关调用图像的信息,但目前我有点卡住了。
谢谢你的帮助!
编辑:我尝试过的新代码:
NSURL *myurl2 = [NSURL URLWithString:@"http://www.myserver.com/bigoaks.png"];
NSURLRequest *request2 = [NSURLRequest requestWithURL:myurl2
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10.0];
NSURLConnection *connection2= [[NSURLConnection alloc] initWithRequest:request2
delegate:self];
NSString *mystring2 = [NSString stringWithContentsOfURL:myurl2 encoding:NSUTF8StringEncoding error:&error];
[mybutton setImage : mystring2];
似乎仍然得到“UIButton 没有可见的@interface 声明选择器'setImage:'
再次感谢所有的帮助!
编辑2:
知道了!感谢所有帮助人员 - 这是帮助其他任何人的最终代码:
NSURL *myurl2 = [NSURL URLWithString:@"http://www.myserver.com/bigoaks.png"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:myurl2]];
[mybutton setImage:image forState:UIControlStateNormal];
使用本教程:http: //mobiledevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html