Domanda

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?

È stato utile?

Soluzione 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)#>];

Altri suggerimenti

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:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top