URL图片问题iphone

时间:2013-01-24 13:10:10

标签: iphone ios objective-c

我有两个主要的小图像按钮说 aBtn &的 bBtn 即可。当我点击 aBtn / bBtn 时,我生成6个图像网址作为默认情况,检查图像不是的条件。在6幅图像中,如果某些图像为空,那么我单独避免使用该空图像。相应地设置框架。当我点击 aBtn到bBtn 时,我有时间延迟。这是由于检查6个url图像每次生成时没有nil /空图像和&通过相应地设置框架位置。

这是我用来显示图像和代码的代码。从6个url图像检查条件数据不是nil如果有人说第3个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++;
    }

1 个答案:

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