iphone + UIscrollview或使用uitableview以矩阵格式显示图像

时间:2012-03-30 05:20:11

标签: iphone uitableview uiscrollview

我想显示图像(来自webservice)的格式,使得屏幕中的输出如下图所示: -

enter image description here

现在我实施的逻辑是,我在uiscrollview中添加了图像。 以下逻辑: -

for (int j=0;j<9;j++) {
        for (int i=0; i<[mainRestaurantArray count];i++) {
            if ([[[mainRestaurantArray objectAtIndex:i] valueForKey:@"Image"] isKindOfClass:[UIImage class]]) {     
                [CombineArray addObject:[mainRestaurantArray objectAtIndex:i]]; 
                //NSLog(@"cnt=>%d   array==>%@",cnt,[CombineArray objectAtIndex:cnt]);
                UIButton* btn = [[UIButton alloc]init];
                btn.tag = cnt;
                btn.frame = CGRectMake(15+(cnt%5)*60, 15+(cnt/5)*60,Width,Height);
                btn.backgroundColor = [UIColor greenColor];
                [btn setBackgroundImage:[[CombineArray objectAtIndex:cnt] valueForKey:@"Image"] forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(Buttonclick:) forControlEvents:UIControlEventTouchUpInside];
                [ScrlPhotos addSubview:btn];
                [btn release];
                cnt++;
            }
        }
        [mainRestaurantArray release];
        counter++;
        [self urlcalled];//The function which calls the webservice
    }   
    //}
    ScrlPhotos.contentSize = CGSizeMake(320, ([CombineArray count]/5.0)*60+25);

进行网络服务的功能如下: -

-(void)urlcalled{   
    @try
    {

        if (rangeDistance == nil) {
            rangeDistance =@"100";
        }


        urlstring[0]=[NSString stringWithFormat:@"http://www.google.com/maps?q=Gym&sll=%@,%@&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
        urlstring[1]=[NSString stringWithFormat:@"http://www.google.com/maps?q=resort&sll=%@,%@&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
        urlstring[2]=[NSString stringWithFormat:@"http://www.google.com/maps?q=Tourist place&sll=%@,%@&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
        urlstring[3]=[NSString stringWithFormat:@"http://www.google.com/maps?q=Hotels&sll=%@,%@&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
        urlstring[4]=[NSString stringWithFormat:@"http://www.google.com/maps?q=shopping Mall&sll=%@,%@&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
        urlstring[5]=[NSString stringWithFormat:@"http://www.google.com/maps?q=Industries&sll=%@,%@&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
        urlstring[6]=[NSString stringWithFormat:@"http://www.google.com/maps?q=Shopping_mall&sll=%@,%@&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
        urlstring[7]=[NSString stringWithFormat:@"http://www.google.com/maps?q=garden&sll=%@,%@&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
        urlstring[8]=[NSString stringWithFormat:@"http://www.google.com/maps?q=Religious Place&sll=%@,%@&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];
        urlstring[9]=[NSString stringWithFormat:@"http://www.google.com/maps?q=Restaurants&sll=%@,%@&radius=200000&output=json",AppDel.Latitude,AppDel.Longitude];



        NSLog(@"conte==>%d  urlstring[counter]==>%@",counter,urlstring[counter]);

        NSString *str = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlstring[counter]]];

        str1 =  [str substringFromIndex:9]; 
        //NSLog(@"===>%@",str1);
        NSArray *array = [str1 componentsSeparatedByString:@"overlays:"];
        NSString *str2 = [array objectAtIndex:1]; 
        NSString *finalUpperStr = [str2 substringFromIndex:21];
        array1 = [finalUpperStr componentsSeparatedByString:@"},panel:"];

        NSString *strF = [array1 objectAtIndex:0];
        NSArray *arrayF = [strF componentsSeparatedByString:@"{id:"];

        ///////....................this object will change as require in for loop .............................//
        mainRestaurantArray = [[NSMutableArray alloc] init];
        for (int i=1; i<[arrayF count]-5; i++) {
            NSString *strF12 = [arrayF objectAtIndex:i]; 
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            NSArray *aryF23 = [strF12 componentsSeparatedByString:@"latlng:{"];
            //NSLog(@"%@",[aryF23 objectAtIndex:1]);
            NSString *strF23 = [aryF23 objectAtIndex:1];
            NSArray *ar = [strF23 componentsSeparatedByString:@"},image:"];
            //NSLog(@"%@",[ar objectAtIndex:0]);
            NSString *strLat = [ar objectAtIndex:0];
            NSArray *arrayLat = [strLat componentsSeparatedByString:@","];
            NSString *strFLat = [[arrayLat objectAtIndex:0] substringFromIndex:4];
            NSString *strLong = [[arrayLat objectAtIndex:1] substringFromIndex:4];
            //NSLog(@"Lati = %@ Longi = %@" , strFLat , strLong);
            [dic setValue:strFLat forKey:@"Latitude"];
            [dic setValue:strLong forKey:@"Longitude"];
            NSString *strLAdd = [ar objectAtIndex:1];
            NSArray *arrayLAdd = [strLAdd componentsSeparatedByString:@"laddr:"];
            //NSLog(@"%@",[arrayLAdd objectAtIndex:1]);
            NSString *strLAdd1 = [arrayLAdd objectAtIndex:1];
            NSArray *arrayLAdd1 = [strLAdd1 componentsSeparatedByString:@"geocode:"];
            //  NSLog(@"%@",[arrayLAdd1 objectAtIndex:0]);
            NSString *strAddres =   [[arrayLAdd1 objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
            //NSLog(@"%@",strAddres);
            [dic setValue:strAddres forKey:@"Address"];
            NSString *strName = [arrayLAdd1 objectAtIndex:1];
            NSArray *arrayName = [strName componentsSeparatedByString:@"name:"];
            NSString *strName1 = [[arrayName objectAtIndex:1]stringByReplacingOccurrencesOfString:@"\"" withString:@""];
            NSArray *arrayName1 = [strName1 componentsSeparatedByString:@"infoWindow:"];

            [dic setValue:[arrayName1 objectAtIndex:0] forKey:@"Name"];
            //            for image load .. 07Dec..

            NSString *strImage= [arrayF objectAtIndex:i];
            NSRange range = [strImage rangeOfString:@"photoUrl"]; 
            if(range.location == NSNotFound){ 

                NSLog(@"Not found");
                [dic setValue:@"" forKey:@"Image"];

            }else{

                NSArray *arrayImage = [strImage componentsSeparatedByString:@"photoUrl:"];


                NSString *strImage1 = [arrayImage objectAtIndex:1];
                NSArray *arrayImage1 = [strImage1 componentsSeparatedByString:@",photoType:"];


                NSString *strfindImage =    [[arrayImage1 objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
                myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strfindImage]]];
                [dic setValue:myImage forKey:@"Image"];
            }

            [mainRestaurantArray addObject:dic];    
            if (i==20) {                    
                break;
            }
        }

    }
    @catch (NSException * e) 
    {
        NSLog(@"NSException==>%@",e);
    }

}

但我面临的问题是在完成循环10次后显示图像。 我希望平行显示图像,因为它来自webservice,所以花费的时间应该更少.. 有什么出路......

请帮帮我。

1 个答案:

答案 0 :(得分:1)

使用UIImageView + cacheWeb ..它使延迟加载变得更加容易 http://hackemist.com/SDWebImage/doc/Categories/UIImageView+WebCache.html