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