简体   繁体   English

如果在iphone中向下滚动scrollview,如何下载和显示服务器图像

[英]how to download and display server images if scrolldown the scrollview in iphone

I am new to iPhone programming. 我是iPhone编程的新手。

Using below code I can able to download and displaying all images form server. 使用以下代码,我可以下载和显示服务器上的所有图像。 But in server I have more than some 1000s of images are there. 但是在服务器中,我有超过数千张图像。 so Using below code I can able to download and displaying in scrollview as 3*3 thumbnail. 所以使用下面的代码,我可以下载并以3 * 3缩略图的形式显示在滚动视图中。

But what I want means first I have to download and display 15 images in scrollview as 3*3 thumbnail. 但是我想要的意思是首先我必须下载并以3 * 3缩略图的形式在滚动视图中显示15张图像。

If I scroll down means i have to show activity indicator then download next form 16 to 30 images, similarly again if I scroll means I want to download and display 31 to 45 images in thumbnail. 如果我向下滚动表示我必须显示活动指示器,然后下载下一个表格16至30张图像,同样,如果我向下滚动则表示我想下载并显示31至45张缩略图图像。

I dont want to download all images form server. 我不想从服务器下载所有图像。

Can any tell me please how can I do this. 有谁能告诉我我该怎么做。

- (void)viewDidLoad
{
    URLs = [[NSMutableArray alloc]init];

    for (NSString *path in latestiamge)
    {
        NSURL *URL = [NSURL URLWithString:path];

        if (URL)
        {
            [URLs addObject:URL];
        }
        else
        {
            NSLog(@"'%@' is not a valid URL", path);
        }
    }

    self.imageURLs = URLs;

    myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 84.0, 320.0, 840.0)];
    myScrollView.delegate = self;
    myScrollView.contentSize = CGSizeMake(320.0, 840.0);
    myScrollView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:myScrollView];

    float horizontal = 2.0;
    float vertical = 2.0;

    for(int i=0; i<[imageURLs count]; i++)
    {
        if((i%3) == 0 && i!=0)
        {
            horizontal = 5.0;
            vertical = vertical + 100.0 + 5.0;
        }

        CGRect frame;
        frame.size.width=100.0;
        frame.size.height=100.0;
        frame.origin.x=0;
        frame.origin.y=0;

        AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:frame];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.tag = i;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self
                                             action:@selector(actionHandleTapOnImageView:)];

        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:singleTap];

        [myScrollView addSubview:imageView];
        [myScrollView addSubview:imageView];

        horizontal = horizontal + 100.0 + 5.0;
    }

    [myScrollView setContentSize:CGSizeMake(320.0, vertical + 3900.0)];

    [super viewDidLoad];
}

Well there is a good and basic class you can use for downloading and displaying images asynchronously . 好了,有一个很好的基础类可以用于异步下载和显示图像。

SDWebImage SDWebImage

Doing everything yourself as you currently are is more effort than is required and not good for memory management. 自己动手做所有事情比所需的工作多,并且不利于内存管理。 Consider using a table or collection view to manage the scrolling so you don't have so many views loaded at the same time and so you don't need code for the full layout of everything. 考虑使用表或集合视图来管理滚动,这样就不会同时加载太多视图,因此不需要代码来呈现所有内容。

You're already using AsyncImageView , it will work if you add a number of them to the cells you're going to display and configure them as requested by the delegate/dataSource methods. 您已经在使用AsyncImageView ,如果您将其添加到要显示的单元格中并根据委托/数据源方法的要求进行配置,它将可以正常工作。

You should also think about acting as the scroll view delegate and monitoring the scroll completion. 您还应该考虑充当滚动视图委托并监视滚动完成。 If the user has scrolled to the current bottom, you could add a footer view with an activity indicator, start a load of the next page from the server and then reload the view and remove the footer when the new page is downloaded. 如果用户滚动到当前底部,则可以添加带有活动指示器的页脚视图,从服务器开始加载下一页,然后重新加载视图并在下载新页面时删除页脚。

From what I can understand from your question, you want to create a image grid with three images in a row. 从我对您的问题的理解中,您想创建一个连续三张图像的图像网格。 But your approach is wrong...! 但是你的方法是错误的...!

Do not use scrollView but use a UITableView. 不要使用scrollView,而要使用UITableView。 I think you can use UICollectionView if you are targeting iOS 6.0 or above. 我认为,如果您的目标是iOS 6.0或更高版本,则可以使用UICollectionView

Here is what you need to do : 这是您需要做的:

  • Create custom UITableViewCell with number of images you need in a row. 创建自定义的UITableViewCell,其中包含连续需要的图像数量。 You can make this dynamic too by passing number of grid items you need while creating the cell and creating the and positioning those views in the cell as subviews. 您也可以在创建单元格时传递所需的网格项目数量,并在其中创建这些视图并将其作为子视图放置在单元格中,以使其动态化。
  • Reuse the cells in the table to populate the grid. 重复使用表格中的单元格以填充网格。
  • You can cache images for better performance, I would suggest you to use SDWebImage 您可以缓存图像以获得更好的性能,我建议您使用SDWebImage
  • In cellForRowAtIndexPath you can configure all the gridItems. cellForRowAtIndexPath您可以配置所有gridItems。

Do you need more help...?? 您是否需要更多帮助...?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM