我只想为IBOutlet
设置图片并为其创建 [_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
}];
。我上面有一个加载器,当加载图像时,加载器将停止并为该按钮分配新图像。调用代码时,占位符图像将保留,并且不会加载URL图像
#define LOCKED 0
#define UNLOCKED 1
#define PRINT(x) (x == LOCKED) ? printf("locked") : printf("unlocked")
答案 0 :(得分:1)
您正在使用完成块从网址下载图片。所以你需要按照以下方式去做。
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[_Image1 setImage:btnImage forState:UIControlStateNormal];//Don't forgot to check for error if error is nil or image is not nil then only set image
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
}];
答案 1 :(得分:0)
如果您正在使用SDWebImageCache
的完成块,请在完成块内设置图像
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
_Image1.image = image;
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
}];
答案 2 :(得分:0)
在主线程中更新图像尝试这样的事情
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
dispatch_sync(dispatch_get_main_queue(), ^{
_Image1.image = image;
});
}];
答案 3 :(得分:0)
从NSData
和NSUrlRequest
"您的网址"获取NSUrl URLWithString
并将NSData
转换为UIImage
并设置背景图片。
答案 4 :(得分:0)
首先获取图片网址并检查图片网址长度是否大于零/ null
。并确保图像网址正常工作。点击url
上的Browser
,然后检查您是否获得了正确的图像。
SDWebImage
库正在更新按钮图片,但Main Thread
上没有发生这种情况。这可能是未更新backgroundImage
的{{1}}的原因。
UIButton
停止- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
forState:(UIControlState)state
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
completed:(nullable SDExternalCompletionBlock)completedBlock {
if (!url) {
[self.imageURLStorage removeObjectForKey:@(state)];
return;
}
NSString *imageURL = [arrayGallery valueForKey:@"link"][0];
self.imageURLStorage[@(state)] = url;
__weak typeof(self)weakSelf = self;
[self sd_internalSetImageWithURL:url
placeholderImage:placeholder
options:options
operationKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]
setImageBlock:^(UIImage *image, NSData *imageData) {
[weakSelf setBackgroundImage:image forState:state];
}
progress:nil
completed:completedBlock];
}
动画并在loader
上设置UIButton
背景图片。
main thread