将图片加载到UIButton中时,图片不会显示

时间:2023-02-10 21:08:18

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:

现在我正在尝试使用AFNetworking加载图片,如下所示:

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:

我尝试过这样的事情:我将插座连接到IB中的imageView并添加了以下代码:

[_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.

再次,只是占位符将显示。

2 个解决方案

#1


1  

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

#2


0  

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) {



                }];

#1


1  

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

#2


0  

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) {



                }];