1

我一直在使用这段代码,它工作正常:

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

现在我正在尝试使用 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];

我得到占位符图像很好,但原始图片不会显示。

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

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

图像将显示正常。但如果我这样做:

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

同样,只会显示占位符。

4

2 回答 2

1

我最终使用了此处添加的用户之一的提交。有了那个提交,它很简单

[button setImageWithURL:[NSURL URLWithString: currentMovie.trailerThumb] placeholderImage:[UIImage imageNamed:@"placeHolder.png"] forState:UIControlStateNormal];
于 2013-05-26T11:23:05.007 回答
0

尝试这个 :

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



                }];
于 2013-05-26T09:24:36.980 回答