【问题标题】:How to implement lazy loading to uiscroll view如何实现延迟加载到uiscrollview
【发布时间】:2013-09-30 09:24:54
【问题描述】:

如何实现uiscroll视图的延迟加载

我正在从网络服务中获取 15 张图像并将其添加到 UIscrollview 我想用临时图像加载滚动视图,当图像准备好时,它可以将图像添加到 uiscrollview

-(void)loadthscroll:(NSMutableArray *)idarray:(NSMutableArray *)imgarray
{



/*************scroll for scroll in in theater******************/

scrollview_intheater= [[UIScrollView alloc] initWithFrame:CGRectMake(0, btn_thhead.frame.size.height,view_banner.frame.size.width, view_intheater.frame.size.height-btn_thhead.frame.size.height)];
[view_intheater addSubview:scrollview_intheater];

[scrollview_thcsoon removeFromSuperview];
adj=5;
currentx=5;
for(int i=0;i<[imgarray count];i++)

{

    btn_theater_ct = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_theater_ct.frame=CGRectMake(currentx, 5, 95, 120);
    [btn_theater_ct addTarget:self action:@selector(theaterAction:) forControlEvents:UIControlEventTouchDown];
    [scrollview_intheater addSubview:btn_theater_ct];
    addr = [imgarray objectAtIndex:i];
    addr = [addr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    //NSLog(@"addr-------%@",addr);
    data_theater = [NSData dataWithContentsOfURL:[NSURL URLWithString:addr ] ];        
    [btn_theater_ct setBackgroundImage:[UIImage imageWithData:data_theater] forState:UIControlStateNormal];
    currentx +=btn_theater_ct.frame.size.width+adj;
    btn_theater_ct.tag=i;
    [scrollview_intheater setContentSize:CGSizeMake(currentx,0)];
    NSLog(@"add th scroll--%d----%d",[imgarray count],i);

}


}

【问题讨论】:

    标签: iphone ios objective-c uiscrollview lazy-loading


    【解决方案1】:

    几天前我遇到了同样的问题,但我现在解决了 导入

    #import "UIImageView+AFNetworking.h"
    

    并实现其方法

    - (void)setImageWithURL:(NSURL *)url  placeholderImage:(UIImage *)placeholderImage;
    

    要使用这个你必须使用 AFNetworking

    【讨论】:

      【解决方案2】:
      1. 使用loading image 设置整个 UIScrollView
      2. 创建一个交换给定图像并在需要时更新内容大小的方法
      3. 添加例程下载图片

      现在您可以设置 UIScrollView 并调用例程进行下载。然后下载完成后调用方法交换图片就完成了。

      【讨论】:

        【解决方案3】:

        尝试研究 GCD(GrandCentralDispatch) 并使用您的场景。 可能这会适合你。

            -(void)loadthscroll:(NSMutableArray *)idarray:(NSMutableArray *)imgarray
            {
        
            scrollview_intheater= [[UIScrollView alloc] initWithFrame:CGRectMake(0, btn_thhead.frame.size.height,view_banner.frame.size.width, view_intheater.frame.size.height-btn_thhead.frame.size.height)];
            [view_intheater addSubview:scrollview_intheater];
        
            [scrollview_thcsoon removeFromSuperview];
            adj=5;
            currentx=5;
        
        
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        //Here, do your iteration part like parsing URL from array and hitting the service which is background thread.            
        
                    for(int i=0;i<[imgarray count];i++)
                  {
                        btn_theater_ct = [UIButton buttonWithType:UIButtonTypeCustom];
                        btn_theater_ct.frame=CGRectMake(currentx, 5, 95, 120);
                        [btn_theater_ct addTarget:self action:@selector(theaterAction:)                                                 forControlEvents:UIControlEventTouchDown];
                        [scrollview_intheater addSubview:btn_theater_ct];
        
                    currentx +=btn_theater_ct.frame.size.width+adj;
                        [scrollview_intheater setContentSize:CGSizeMake(currentx,0)];
        
                    NSLog(@"add th scroll--%d----%d",[imgarray count],i);
                    addr = [imgarray objectAtIndex:i];
                        addr = [addr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
                        //NSLog(@"addr-------%@",addr);
                        data_theater = [NSData dataWithContentsOfURL:[NSURL URLWithString:addr ] ];   
                    UIImage *receivedImage = [UIImage imageWithData:data_theater];
                    }
                        dispatch_async(dispatch_get_main_queue(), ^{
        
        //Here, do your UI work like set the background image which is include in main thread                    
        
                            if([receivedImage isKindOfClass:[UIImage class]])
                            {
                                [btn_theater_ct setBackgroundImage: receivedImage];
                            btn_theater_ct.tag=i;
                            }
                        });
        
                });
        
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多