首先得到圖片的url並檢查圖片的url長度是否大於零/不是null
。並確保圖像網址工作正常與否。點擊Browser
上的url
,然後檢查您是否獲得了正確的圖像。
SDWebImage
庫正在更新按鈕圖像,但這不會發生在Main Thread
上。這可能是不更新UIButton
的backgroundImage
的原因。
- (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
停止動畫和上main thread
設置UIButton
背景圖像。
NSString *imageURL = [arrayGallery valueForKey:@"link"][0];
if (imageURL.length > 0) {
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
dispatch_async(dispatch_get_main_queue(), ^{
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
[_Image1 setBackgroundImage:image forState:UIControlStateNormal];
});
}];
}
不能使用'_Image1.image =圖像'作爲按鈕 – meowsush
@meowsush檢查我編輯的答案,如果它的工作標記爲接受的答案 – vivek