简体   繁体   English

SDWebImage不显示下载图像

[英]SDWebImage not displaying download images

I used SDWebImage library for downloading remote images from server. 我使用SDWebImage库从服务器下载远程图像。 But while im in EDGE connection it taking too much time and after downloading the NSLog values are displaying but images are not showing under UIButton 但是当我在EDGE连接中时,它花费了太多时间,并且在下载NSLog值后却显示了,但是UIButton下没有显示图像

code: 码:

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

Maybe there is an error or a timeout. 可能有错误或超时。 Try this change and maybe we can find more: 尝试此更改,也许我们可以找到更多:

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

Heres something that worked for me, I keep the loop but I don't use it so it matches your code. 这是对我有用的东西,我保留了循环,但我不使用它,因此它与您的代码匹配。 I declared imgView1 to be a UIButton and I hardcoded a URL to a stackoverflow image. 我宣布imgView1为UIButton,并将URL硬编码为stackoverflow图像。 I recommend you check the type of imgView1 and the content of productimg_array 我建议您检查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];

}

You can use this method from UIButton+WebCache.h 您可以从UIButton+WebCache.h使用此方法

- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state;

Calling this is similar, 称呼类似

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

This method will download the image and directly set it to the button. 此方法将下载图像并将其直接设置为按钮。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM