Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to programmatically set the background on a UICustomButton?
    primarykey
    data
    text
    <p>I know this question has been asked and answered in other forums, but I can't seem to duplicate what they did and get it to work. I am trying to set the background to a button using a picture that I download from a server. Everything works until I try to make a NSData object with the contents of a URL. When I try and print the Data to the screen it comes back Null. What am I doing wrong? </p> <pre><code>myImageURLString = [[NSMutableString alloc] initWithString:@"http://destinationnexus.com"]; keyName = [businessButtonTagArray objectAtIndex:i]; arrayFromBusinessDictionary = [businessDictionary objectForKey:keyName]; stringToAddToMyImageURLString = [arrayFromBusinessDictionary objectAtIndex:1]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; [button setTag:i]; button.frame = CGRectMake(xOffSet+(countForX*xMult), yOffset +(countForY*yMult), 215, 155); [myImageURLString appendString:stringToAddToMyImageURLString]; NSLog(@"Appended URL String: = %@", myImageURLString); NSURL *myImageURL = [NSURL URLWithString:@"http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729//.jpg"]; NSLog(@"NSURL = %@", myImageURL); //THIS IS WHERE I SEEM TO HAVE A PROBLEM..... NSData *imageData = [[NSData alloc] initWithContentsOfURL:myImageURL]; UIImage *image = [[UIImage alloc] initWithData:imageData] ; NSLog(@"Image Data = %@", imageData); //UIImage *myImage = [UIImage imageWithData:imageData] ; [button setBackgroundImage:image forState:UIControlStateNormal]; </code></pre> <p>When I run the program this is what it outputs in the Log:</p> <p>Appended URL String: = <a href="http://destinationnexus.com/pictures/directory/Auburn-University-Hotel-and-Center-in-Auburn-Alabama-36830-5429.jpg" rel="nofollow">http://destinationnexus.com/pictures/directory/Auburn-University-Hotel-and-Center-in-Auburn-Alabama-36830-5429.jpg</a></p> <p>NSURL = <a href="http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729.jpg" rel="nofollow">http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729.jpg</a></p> <p>Image Data = (null)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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