Iphone 将URL图像添加到按钮单击
我在一些教程中搜索了将UIImage添加到click事件的按钮。如何添加URL路径 图像到按钮..例如代码:Iphone 将URL图像添加到按钮单击,iphone,objective-c,image,url,uibutton,Iphone,Objective C,Image,Url,Uibutton,我在一些教程中搜索了将UIImage添加到click事件的按钮。如何添加URL路径 图像到按钮..例如代码: UIButton *imageView=[[UIButton alloc]initWithFrame:CGRectMake((320*index)+countFlag*80+ 2, 5, 75, 75)]; imageView.tag=i+1; [imageView addTarget:self action:@select
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;
}
为ur按钮(imageView)尝试此操作
对于正常状态
[imageView setImage:[UIImage imageNamed:@"img1.png"] forState:UIControlStateNormal];
对于单击的(选定的)状态
希望这会有所帮助。试试这个:
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];
最好使用这个
UIButton+WebCache.h
类别,它支持异步加载按钮的图像。您可以使用以下任何方法
- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state;
- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder;
它还为ui按钮
- (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state;
- (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder;
不要忘记将SDWebImage
包含到项目中,并将UIButton+WebCache.h
文件包含到实现文件中
参考:要显示带有背景图像的按钮吗?使用EGOButton作为URL。使用哪个ios版本r us?为imageview或uibutton设置图像?使用ios 6.1,将图像设置为uibutton。UIButton*imageView=[[UIButton alloc]initWithFrame:CGRectMake((320*index)+countFlag*80+2,5,75,75)];tag=i+1;[imageView添加目标:自我操作:@选择器(imageView单击:)用于控制事件:UIControlEventTouchUpInside];[imageView.layer setBorderColor:[UIColor lightGrayColor].CGColor];[imageView.layer-Width:1.0f];它起作用了。谢谢..那么是否可以将多个url加载到uibutton?
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];
- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state;
- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder;
- (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state;
- (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder;