SDWebImage不显示下载图像

时间:2013-09-24 06:46:16

标签: iphone uiscrollview uiimageview uibutton sdwebimage

我使用SDWebImage库从服务器下载远程图像。但是,虽然我在EDGE连接中花费了太多时间并且在下载后显示NSLog值,但图片未显示在UIButton

代码:

int Width = 0;

        for (int i = 0; i<[productimg_array count]; i++ ) {

            NSLog(@"index %@", productimg_array[i]);

            imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];

            Width = Width + 20+(i*74);

            [imgView1 setTag:i+1];

            [imgView1 addTarget:self action:@selector(dbClicked:) forControlEvents:UIControlEventTouchUpInside];

            // [imgView1 setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]]
            //   forState:UIControlStateNormal];

           [imgView1 setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]]  forState:UIControlStateNormal completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {

            NSLog(@"download images 1");
          }];

          [scrollview addSubview:imgView1];

        }

[scrollview setContentSize:CGSizeMake(Width,imgView1.frame.size.height+20)];

3 个答案:

答案 0 :(得分:3)

可能存在错误或超时。试试这个改变,也许我们可以找到更多:

[imgView1 setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]]  forState:UIControlStateNormal completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    if(error){
        NSLog(@"Callback Error:%@",error);
    }
    if(image){
        NSLog(@"Callback Image:%@",image);
    }
}];

答案 1 :(得分:0)

继承了对我有用的东西,我保留了循环,但我没有使用它,所以它匹配你的代码。我将imgView1声明为UIButton,并将URL硬编码为stackoverflow图像。我建议你检查imgView1的类型和productimg_array的内容

int Width = 0;
UIButton *imgView1;
for (int i = 0; i<1; i++ ) {


    imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];

    Width = Width + 20+(i*74);

    [imgView1 setTag:i+1];

    [imgView1 addTarget:self action:@selector(cancelPressed:) forControlEvents:UIControlEventTouchUpInside];

    [imgView1 setImageWithURL:[NSURL URLWithString:@"http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6"]  forState:UIControlStateNormal completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {

        NSLog(@"download images 1");
    }];

    [self.view addSubview:imgView1];

}

答案 2 :(得分:0)

您可以使用UIButton+WebCache.h

中的此方法
- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state;

调用此类似,

[imgView1 setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]] forState:UIControlStateNormal];

此方法将下载图像并直接将其设置为按钮。

希望有所帮助!