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

Iphone 如何实现对uiscroll视图的延迟加载,iphone,ios,objective-c,uiscrollview,lazy-loading,Iphone,Ios,Objective C,Uiscrollview,Lazy Loading,如何实现对uiscroll视图的延迟加载 我正在从web服务中拍摄15幅图像,并将其添加到UIscrollview 我想在滚动视图中添加临时图像,当图像准备好后,它可以将图像添加到uiscrollview -(void)loadthscroll:(NSMutableArray *)idarray:(NSMutableArray *)imgarray { /*************scroll for scroll in in theater******************/ scr

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

我正在从web服务中拍摄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);

}


}
-(void)loadthscroll:(NSMutableArray*)idarray:(NSMutableArray*)imgarray
{
/*************剧院里的滚动******************/
scrollview_-intheter=[[UIScrollView alloc]initWithFrame:CGRectMake(0,btn_-thhead.frame.size.height,view_-banner.frame.size.width,view_-intheter.frame.size.height-btn_-thhead.frame.size.height)];
[在水中查看添加子视图:在水中滚动查看];
[scrollview_thcsoon从SuperView中移除];
adj=5;
电流x=5;
对于(int i=0;i
  • 使用
    加载图像设置整个UIScrollView
  • 创建一个交换给定图像的方法,并根据需要更新内容大小
  • 添加一个例程并下载图像
  • 现在,您可以设置UIScrollView并调用例程进行下载。下载完成后,调用方法交换图像,您就完成了。

    尝试研究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;
                        }
                    });
    
            });
    
        }
    
    -(void)loadthscroll:(NSMutableArray*)idarray:(NSMutableArray*)imgarray
    {
    scrollview_-intheter=[[UIScrollView alloc]initWithFrame:CGRectMake(0,btn_-thhead.frame.size.height,view_-banner.frame.size.width,view_-intheter.frame.size.height-btn_-thhead.frame.size.height)];
    [在水中查看添加子视图:在水中滚动查看];
    [scrollview_thcsoon从SuperView中移除];
    adj=5;
    电流x=5;
    调度异步(调度获取全局队列(调度队列优先级后台,0)^{
    //在这里,执行迭代部分,比如解析数组中的URL并点击作为后台线程的服务。
    
    对于(int i=0;i我在几天前就遇到了同样的问题,但我现在已经解决了 进口

    #import "UIImageView+AFNetworking.h"
    
    并实施其方法

    - (void)setImageWithURL:(NSURL *)url  placeholderImage:(UIImage *)placeholderImage;
    
    要使用此功能,您必须使用AFNetworking类