iphone+UIscrollview 或者使用uitableview以矩阵格式显示图片 [英] iphone+UIscrollview or to use uitableview to display image in matrix format

查看:18
本文介绍了iphone+UIscrollview 或者使用uitableview以矩阵格式显示图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示图像(来自网络服务)的格式,以便屏幕中的输出如下图所示:-

I want to display the image(which comes from webservice) the format such that the output in the screen appears like below image:-

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

Now the logic i had implemented is,i had added the image in uiscrollview. with the below logic:-

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);

执行webservice的函数如下:-

The function which does the webservice is below:-

-(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 次循环后显示.我想并行显示来自网络服务的图像,这样花费的时间应该更少..有什么出路吗..

But the problem i am facing is the image is displayed after completing the loop at 10 times. I want to display the image parallely as it comes from webservice,so that time taken should be less.. Is there any way out..

请帮帮我.

推荐答案

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

Use UIImageView+cacheWeb.. it makes lazy loading a lot easier http://hackemist.com/SDWebImage/doc/Categories/UIImageView+WebCache.html

这篇关于iphone+UIscrollview 或者使用uitableview以矩阵格式显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆