Note that there are some explanatory texts on larger screens.

plurals
  1. POHow To Set Image From Server As Background Image Of Rect Button
    text
    copied!<p>I've been trying to set the background image of a rect button as a .png file I have on my web server. I've declared the button in the view controller .h and added this under .m viewdidload:</p> <pre><code>[mybutton setImage: [UIImage imageNamed: @"http://www.myserver.com/index_files/stacks_image_14.png"] forState: UIControlStateHighlighted]; </code></pre> <p>but nothing shows in the button. I've been reading on NSSURL to see about calling in images before I try to assign them to objects, but I'm a little stuck at the moment.</p> <p>Thanks for any help!</p> <p>EDIT: New Code I've tried:</p> <pre><code> 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:&amp;error]; [mybutton setImage : mystring2]; </code></pre> <p>Still seem to get "No visible @interface for UIButton declares the selector 'setImage:'</p> <p>Thanks again for all the assistance!</p> <p>EDIT 2:</p> <p>Got it! Thanks for all of the help guys-- here's the final code to help any others:</p> <pre><code>NSURL *myurl2 = [NSURL URLWithString:@"http://www.myserver.com/bigoaks.png"]; UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:myurl2]]; [mybutton setImage:image forState:UIControlStateNormal]; </code></pre> <p>Using this tutorial: <a href="http://mobiledevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html" rel="nofollow">http://mobiledevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload