0

我正在使用给定代码从其各自的 url 下载图像,但由于某些问题,它无法正常工作,我无法诊断问题。请帮我解决这个问题。提前致谢。

-(void)createScrollViewControls
{
    [self removeAllControlsFromScrollView];

    totalNumberOfImages=[arrImages count];       

    __block int x=0;
    __block int y=0;
    __block int scroll=0;
    __block UIImage *tempimag=nil;

    if(totalNumberOfImages>0) //If product images are there
    {
//        imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT);

        for(int i=0;i<totalNumberOfImages;i++){

            NSLog(@"Value=%@",arrImages);

            UIActivityIndicatorView *activity= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(77+scroll, 38, 30, 30)];
            [activity setBackgroundColor:[UIColor clearColor]];
            [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
            [imageScroll addSubview:activity];
            [activity startAnimating];              

            dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_async(concurrentQueue, ^{

             NSURL *imageURL=[NSURL URLWithString:[arrImages objectAtIndex:i]];   

                NSData *tempData=[NSData dataWithContentsOfURL:imageURL];
                UIImage *tImage=[UIImage imageWithData:tempData];

                if (tImage==nil)
                {
                    tImage=[UIImage imageNamed:@"NoImage.png"];
                }
                dispatch_async(dispatch_get_main_queue(), ^{


                    tempimag= tImage;

                    UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(x, y, IMAGEWIDTH, IMAGEHEIGHT)];

            //if url is there but image not getting downloaded then show a static image
                    if (!tempimag)
                    {
                        imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT);

                        NSLog(@"no underlying data");
                        imageView.image = [UIImage imageNamed:kNoImageAvailable];
                        [imageScroll addSubview:imageView];
                       // totalNumberOfImages = 0;
                    }
                    else{

                        [activity stopAnimating];
                        imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT);
                        imageView.image=tempimag;
                        [imageScroll addSubview:imageView];
                        x=x+imageScroll.frame.size.width;

                    }    

                });

            });

/*
            UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(x, y, IMAGEWIDTH, IMAGEHEIGHT)];



     if (!tempimag)//if url is there but image not getting downloaded then show a static image
            {
                imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT);

                NSLog(@"no underlying data");
                imageView.image = [UIImage imageNamed:kNoImageAvailable];
                [imageScroll addSubview:imageView];
                totalNumberOfImages = 0;
                break;
            }
            else{
                imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT);
                imageView.image=tempimag;
                [imageScroll addSubview:imageView];
                x=x+imageScroll.frame.size.width;   
            }
        } */

            scroll+=184;
    }
}
4

1 回答 1

0

这下面的代码行:

if (tImage==nil)
            {
                tImage=[UIImage imageNamed:@"NoImage.png"];
            }

将始终使您的程序控制转到:

if (!tempimag)
                {
                    imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT);

                    NSLog(@"no underlying data");
                    imageView.image = [UIImage imageNamed:kNoImageAvailable];
                    [imageScroll addSubview:imageView];
                   // totalNumberOfImages = 0;
                }

请删除提到的代码的第一部分并尝试一下。

于 2013-04-12T06:37:34.040 回答