异步下载图像

时间:2013-04-12 05:51:44

标签: iphone ios asynchronous xcode4.5 imagedownload

我正在使用“给定代码”从其各自的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;
    }
}

1 个答案:

答案 0 :(得分: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;
                }

请删除所提及代码的第一部分并进行试用。