0

我只想为 a 设置图像UIButton并为它创建一个IBOutlet。我上面有一个加载器,当加载图像时,加载器将停止并为该按钮分配一个新图像。调用代码时,占位符图像仍然存在,并且不加载 url 图像

  [_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];
    }];
4

5 回答 5

1

您正在使用完成块从 url 下载图像。因此,您需要按照以下方式进行操作。

 [_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];
  }];
于 2017-06-30T07:47:11.070 回答
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];
    }];
于 2017-06-30T07:42:08.287 回答
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;   
        });

 }];
于 2017-06-30T07:43:51.373 回答
0

NSData从“你的网址”获取NSUrlRequest和获取NSUrl URLWithString

并转换NSDataUIImage并设置背景图像。

于 2017-06-30T07:47:20.127 回答
0

首先获取图片 url 并检查图片 url 长度是否大于零null。并确保图像 url 是否正常工作。点击url打开Browser并检查您是否获得正确的图像。

SDWebImage库正在更新按钮图像,但这并没有发生在Main Thread. 这可能是不更新的backgroundImage原因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

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

}
于 2017-06-30T08:19:51.557 回答