2013-03-21 173 views
3

我搜索了一些教程添加UIImage按鈕的點擊事件。如何URL路徑添加添加URL圖片按鈕點擊

圖像button..For示例代碼:

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

你想顯示帶背景圖像的按鈕? – Balu 2013-03-21 11:56:14

+0

使用EGOButton作爲URL。 – 2013-03-21 11:59:08

回答

2

嘗試此烏爾按鈕(ImageView的)

[imageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"imageurl"]]]forState:UIControlStateNormal]; 
+0

這不起作用。有錯誤Setimage棄用 – Ram 2013-03-25 06:20:55

+0

其中ios版本r我們使用?,您設置imageview或uibutton圖像? – NAZIK 2013-03-25 06:25:55

+0

使用ios 6.1,將圖像設置爲uibutton。 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]; – Ram 2013-03-25 06:28:46

1
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"url"]; 
[imageView setImage:[UIImage imageWithData:data]]; 
2

用於正常狀態,

[imageView setImage:[UIImage imageNamed:@"img1.png"] forState:UIControlStateNormal]; 

和點擊(選定)狀態,

[imageView setImage:[UIImage imageNamed:@"img2.png"] forState:UIControlStateSelected]; 

希望這會有所幫助。

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

其更好地利用這個UIButton+WebCache.hSDWebImage它支持圖片的按鈕所處的異步加載。您可以使用以下任何方法。

- (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文件到您的實現文件。

參考:https://github.com/rs/SDWebImage