문제

I am setting image to UIImageView as below.

[pPostImage setImageWithURL:[NSURL URLWithString:@"http://www.mywebsite.com/p.png"  placeholderImage:[UIImage imageNamed:@"loader.png"]];

Now what I wanted is to add button and set its image (so that image is not stretched).

For UIButton Image not to stretch, I found I need to use below statement.

[myButton setImage:(UIImage *) forState:<#(UIControlState)#>];

But how can I write the cached image in setImage?

도움이 되었습니까?

해결책 2

First time only it download image from URL. Twice you called it brings the image from Cache Memory. Because it stores the image with key as your imageUrl. This is the simple way to set image to your button

[myButton setImage:pPostImage.image forState:<#(UIControlState)#>];

다른 팁

If you want to set the image or background image of an UIButton via SDWebImage, you should use the following codes:

Import the relevant library:

#import <SDWebImage/UIButton+WebCache.h>

Then, use this line to ask SDWebImage to download the image for UIButton:

[myButton sd_setImageWithURL:[NSURL URLWithString:@"IMAGE_URL_HERE"] forState:UIControlStateNormal];

similar for background image, use sd_setBackgroundImageWithURL:

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top