简体   繁体   中英

Picture won't show when loading it into a UIButton

I've been using this code and it's working fine:

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: currentMovie.trailerThumb]];        
[button setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
[_moviesScroll addSubview:button];

Now I'm trying to load the pictures using AFNetworking like this:

UIImageView *testMe = [[UIImageView alloc]init];
[testMe setImageWithURL:[NSURL URLWithString:currentMovie.trailerThumb] placeholderImage:[UIImage imageNamed:@"placeHolder.png"]];        
[button setBackgroundImage:testMe.image forState:UIControlStateNormal];
[_moviesScroll addSubview:button];

I get the place holder image fine but the original picture won't show.

I tried doing something like this: I connected an outlet to an imageView in the IB and added this code:

[_test setImageWithURL:[NSURL URLWithString:@"https://www.url.com/pic.png"] placeholderImage:[UIImage imageNamed:@"placeHolder.png"]];

The image will show fine. But if i'll do this:

[button setBackgroundImage:_test.image forState:UIControlStateNormal];

Again, just the place holder will show.

I ended up using a commit one of the users added here . With that commit it's simple as

[button setImageWithURL:[NSURL URLWithString: currentMovie.trailerThumb] placeholderImage:[UIImage imageNamed:@"placeHolder.png"] forState:UIControlStateNormal];

try this :

NSURL * photoUrl = [NSURL URLWithString:@"https://www.url.com/pic.png"];

                NSURLRequest*request = [NSURLRequest requestWithURL:photoUrl];

                __weak typeof(UIImageView) *weak_testImage = _testImage;



                [_testImage setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"placeHolder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                    weak_testImage.image = image;

                    [button setBackgroundImage:_testImage.image forState:UIControlStateNormal];



                } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {



                }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM