1

我有 2 个主要的小图像按钮,比如aBtnbBtn。当我单击aBtn / bBtn时,我默认生成 6 个图像 url,检查图像不为零的条件。在 6 幅图像中,如果某些图像是空的,那么我会单独避免空图像并相应地设置框架。当我从aBtn 点击到 bBtn 时,我有时间延迟。这是由于每次检查生成的 6 个 url 图像而没有 nil/empty 图像并相应地设置框架位置。

这是我用来显示图像的代码并检查 6 个 url 图像数据的条件不是 nil 如果有人说 3rd url 图像是 nil,它将被避免。如何克服显示图像的延迟?

    NSMutableString *strUrl;
    int i;
    int j=7;
    int k=0;
    xorigin=350;
    int cou=1;

    for( i=1;i<j;i++){

        strUrl=[[NSMutableString alloc]init];
        UIImageView *imageView= [[UIImageView alloc]init];


        [strUrl appendFormat:@"http://commondatastorage.googleapis.com/images2.solestruck.com/%@%@%@%@%@%@%@)-01060%d.jpg",
         [[[[shoeDetailDict objectForKey:@"vendorName"] lowercaseString] lowercaseString] stringByReplacingOccurrencesOfString:@" " withString:@"-"],@"-shoes/",[[shoeDetailDict objectForKey:@"vendorName"] stringByReplacingOccurrencesOfString:@" " withString:@"-"],@"-shoes-",[[shoeDetailDict objectForKey:@"productName"] stringByReplacingOccurrencesOfString:@" " withString:@"-"],@"-(",[[prdcolorimgArray objectAtIndex:tagedColor] stringByReplacingOccurrencesOfString:@" " withString:@"-"],i];

        NSURL *imageUrl=[[NSURL alloc]initWithString:strUrl];

        NSError *error;
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strUrl] options:NSDataReadingUncached error:&error];
        NSLog(@"imageurl colors : %@",strUrl);

        if(i==4){

            imageView.frame =CGRectMake(25, 0, (self.view.frame.size.width*80)/100,(self.view.frame.size.height*40)/100);
            [imageView setImageWithURL:imageUrl];
        }

        else{

            if(data != nil ){
                cou++;
                imageView.frame =CGRectMake(xorigin, 0, (self.view.frame.size.width*80)/100,(self.view.frame.size.height*40)/100);
                [imageView setImageWithURL:imageUrl];
                xorigin=xorigin+320;

            }
        }
        imageView.backgroundColor=[UIColor whiteColor];
        [productScroll addSubview:imageView];
        productScroll.contentSize = CGSizeMake((self.view.frame.size.width)*cou, (self.view.frame.size.height*40)/100);
        productScroll.contentOffset=CGPointMake(0, 0);

        k++;
    }
4

1 回答 1

3

您正在dataWithContentsOfURL:同步使用该负载信息。

尝试首先加载所有图像,然后将它们放在滚动视图上。

尝试使用此代码:

- (void) prepareImages {
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        NSMutableArray * imagesArray = [NSMutableArray array];
        // Load all images here
        [self imagesLoaded:imagesArray]
    });
}
- (void) imagesLoaded:(NSArray*) images {
     dispatch_async(dispatch_get_main_queue(), ^{
        //Display images here
    });
}
于 2013-01-24T13:17:20.583 回答