简体   繁体   English

如何以编程方式在UICustomButton上设置背景?

[英]How to programmatically set the background on a UICustomButton?

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. 一切正常,直到我尝试使用URL的内容制作NSData对象。 When I try and print the Data to the screen it comes back Null. 当我尝试将数据打印到屏幕上时,它返回Null。 What am I doing wrong? 我究竟做错了什么?

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];

When I run the program this is what it outputs in the Log: 当我运行程序时,这是它在日志中输出的内容:

Appended URL String: = http://destinationnexus.com/pictures/directory/Auburn-University-Hotel-and-Center-in-Auburn-Alabama-36830-5429.jpg 附加的URL字符串:= http://destinationnexus.com/pictures/directory/Auburn-University-Hotel-and-Center-in-Auburn-Alabama-36830-5429.jpg

NSURL = http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729.jpg NSURL = http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729.jpg

Image Data = (null) 图片数据=(空)

You should correct your myImageURL, by deleting 2 slashes at the end of URL. 您应该通过删除URL末尾的2个斜杠来更正myImageURL。

The code goes something like this. 该代码是这样的。

NSURL *myImageURL = [NSURL URLWithString:@"http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729.jpg"];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:myImageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData] ;
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
[imageData release];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM