我搜索了一些教程,将UIImage添加到点击事件按钮。如何添加网址路径
图像到按钮。例如代码:
UIButton *imageView=[[UIButton alloc]initWithFrame:CGRectMake((320*index)+countFlag*80+ 2, 5, 75, 75)];
imageView.tag=i+1;
[imageView addTarget:self action:@selector(imageViewClicked:) forControlEvents:UIControlEventTouchUpInside];
[imageView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[imageView.layer setBorderWidth:1.0f];
switch ((i+1)%5) {
case 0:
[imageView setImage:[UIImage imageNamed:@"img1.png"] forState:UIControlStateNormal];
break;
case 1:
[imageView setImage:[UIImage imageNamed:@"img2.png"] forState:UIControlStateNormal];
break;
}
答案 0 :(得分:3)
最好使用此UIButton+WebCache.h
类SDWebImage它支持异步加载按钮的图像。您可以使用以下任何一种方法。
- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state;
- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder;
它还为UIButton
- (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state;
- (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder;
不要忘记将SDWebImage
包含在您的项目中,将UIButton+WebCache.h
文件包含在您的实施文件中。
答案 1 :(得分:2)
尝试使用此按钮(imageView)
[imageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"imageurl"]]]forState:UIControlStateNormal];
答案 2 :(得分:2)
对于正常状态,
[imageView setImage:[UIImage imageNamed:@"img1.png"] forState:UIControlStateNormal];
和Clicked(Selected)状态,
[imageView setImage:[UIImage imageNamed:@"img2.png"] forState:UIControlStateSelected];
希望这会有所帮助。
答案 3 :(得分:1)
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"url"];
[imageView setImage:[UIImage imageWithData:data]];
答案 4 :(得分:0)
试试这个:
EGOImageButton* btnUserImage = [[EGOImageButton alloc] initWithPlaceholderImage:[UIImage imageNamed:@"ProfilePictureDefault.png"]];
btnUserImage.frame = CGRectMake(10,5,25,25);
btnUserImage.highlighted = NO;
[self addSubview:btnUserImage];
btnUserImage.imageURL = [NSURL URLWithString:Url];