Ios 图片赢得';将其加载到UIButton时不显示
我一直在使用此代码,它运行良好:Ios 图片赢得';将其加载到UIButton时不显示,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我一直在使用此代码,它运行良好: NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: currentMovie.trailerThumb]]; [button setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal]; [_moviesScroll addSub
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: currentMovie.trailerThumb]];
[button setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
[_moviesScroll addSubview:button];
现在,我尝试使用以下方式加载图片:
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];
同样,只显示占位符。尝试以下操作:
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) {
}];
试试这个:
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) {
}];
最后,我使用了一个用户添加的提交。 有了这个承诺,就很简单了
[button setImageWithURL:[NSURL URLWithString: currentMovie.trailerThumb] placeholderImage:[UIImage imageNamed:@"placeHolder.png"] forState:UIControlStateNormal];
最后,我使用了一个用户添加的提交。 有了这个承诺,就很简单了
[button setImageWithURL:[NSURL URLWithString: currentMovie.trailerThumb] placeholderImage:[UIImage imageNamed:@"placeHolder.png"] forState:UIControlStateNormal];
为什么不把变量命名为
ololo1
和ololo2
?哈哈!!我最终使用了一个提交,其中一个用户添加了:通过该提交,它很简单,如[button setImageWithURL:[NSURL URLWithString:currentMovie.trailerThumb]占位符图像:[UIImage ImageName:@“placeHolder.png”]for状态:UIControlStateNormal]
只需确保setImageWithURL不使用同步查询,否则在加载图像之前,您的应用程序将被阻止。为什么不将变量命名为ololo1
和ololo2
?哈哈!!我最终使用了一个提交,其中一个用户添加了:通过该提交,它很简单,如[button setImageWithURL:[NSURL URLWithString:currentMovie.trailerThumb]占位符图像:[UIImage ImageName:@“placeHolder.png”]for状态:UIControlStateNormal]代码>只需确保setImageWithURL不使用同步查询,否则在加载图像之前,您的应用程序将被阻止。“coco”是编程语言,“coco”是编程语言更新为:[按钮setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:currentMovie.trailerThumb]占位符图像:@“placeHolder.png”]]; 代码>已尝试[\u button.imageView setImageWithURL:[NSURL URLWithString:imgURL]]代码>,但不工作更新为:[按钮设置ImageForState:UIControlStateNormal withURL:[NSURL URLWithString:currentMovie.trailerThumb]占位符图像:@“placeHolder.png”]代码>已尝试[\u button.imageView setImageWithURL:[NSURL URLWithString:imgURL]]代码>,但不工作