1

我使用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)];
4

3 回答 3

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);
    }
}];
于 2013-09-24T09:11:05.510 回答
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];

}
于 2013-09-24T08:09:08.270 回答
0

您可以使用此方法从UIButton+WebCache.h

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

调用这个是类似的,

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

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

希望有帮助!

于 2013-09-24T08:09:36.123 回答