[英]Setting UIImage as background of a UIButton
Whats wrong with the following piece of code: 以下代码段有什么问题:
UIButton *button = [[UIButton alloc]init];
CGRect frame = CGRectMake(180, 10, 33, 33);
button.frame = frame;
button.tag = 1001;
UIImage *image = [[[UIImage alloc]init]autorelease];
image = [UIImage imageNamed:@"icon.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
[button release];
If this is wrong where does it need correction and why? 如果这是错误的,则在哪里需要更正?为什么?
I see several problems: 我看到几个问题:
UIImage *image = [UIImage imageNamed:@"icon.png"];
UIImage *image = [UIImage imageNamed:@"icon.png"];
and delete UIImage *image = [[[UIImage alloc]init]autorelease];
UIImage *image = [[[UIImage alloc]init]autorelease];
and [image release];
[image release];
statement. You can try this 你可以试试这个
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(180, 10, 33, 33);;
button.tag = 1001;
UIImage *image = [UIImage imageNamed:@"icon.png"];
if (image == nil) {
NSLog(@"can't find icon.png");
} else {
[button setBackgroundImage:image forState:UIControlStateNormal];
}
//add button to the parent's view
您确定image
不是nil
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.