从网站获取图像列表并显示它 [英] Getting a list of Images from a website and displaying it

查看:27
本文介绍了从网站获取图像列表并显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 iOS 应用程序,它假设获取网站上显示的图像列表并将它们显示为屏幕上的图块(类似于从照片库中显示图像).我可以在 UIImageView 中显示来自 URL 的单个图像,但是在显示来自网站的完整图像列表时,我一无所知.任何正确方向的帮助或任何有用的教程将不胜感激.

I am creating an iOS application that is suppose to get the list of images present on the website and display them as tiles on the screen(similar to the displaying of images from photo library). I can display a single image from a URL in UIImageView but when it comes to displaying the complete list of images from a website, i am clueless. Any help in the right direction or any useful tutorial will be appreciated.

推荐答案

这个问题有两个部分.我会给你一个快速的回复.

There are two parts to this question. I'll give you a quick response to each one.

1:下载图片列表

这可能与使用 +arrayWithContentsOfURL 一样直接:如果您可以完全控制您的网站.一定要分派下载以避免阻塞UI线程.

This could be as straight forward as using +arrayWithContentsOfURL: if you have complete control over your website. Be sure to dispatch the downloading to avoid blocking the UI thread.

NSURL *URL = self.URLOfImages;
dispatch_queue_t dispatch = dispatch_queue_create("fetch.images", NULL);
dispatch_async(dispatch, ^{
    NSArray *images = [NSArray arrayWithContentsOfURL:URL];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.images = images;
    });
});
dispatch_release(dispatch);

注意:URL 必须返回 NSArray 期望的确切格式,并且 self.images 必须通知视图它需要重新加载自身,即.[self.tableView reloadData].

NOTE: The URL must return the exact format expected by NSArray and self.images must notify the view that it needs to reload itself ie. [self.tableView reloadData].

如果您对 URL 的控制较少,则需要使用 NSURLRequest 之类的东西创建请求,使用 NSURLConnection 之类的东西获取数据,并从响应中解析出图像列表以创建图像数组.

If you have less control over the URL, you will need to create a request using something like NSURLRequest, fetch the data using something like NSURLConnection, and parse out the the list of images from the response to create the array of images.

在更复杂的场景中,将涉及分页.这将只需要下载部分列表,然后随着需要更多图像请求更多图像.

In a much more complex scenario, paging will be involved. This will require only downloading some of the list, then as more images are needed requesting more images then.

2:显示图片列表

这可能就像使用 UITableView 和使用 UITableViewCellStyleDefault 样式的 UITableViewCells 一样简单,然后在每个单元格中设置 imageView 属性.

This could be as simple as using a UITableView with UITableViewCells that use the UITableViewCellStyleDefault style, then set the imageView property in each cell.

为了更好地查看图像,您可以编写使用从 UITableViewCell 子类化的自定义表格视图单元格.这将允许您使用图像的自定义位置并将图像的大小设置为您想要的大小.

To get a better view of the image you could write use a custom table view cell subclassed from UITableViewCell. This would allow you to use a custom placement of the image and set the size of the image to what ever you would like.

在更复杂的场景中,您可以创建自己的自定义视图,其中包含可放置所有图像的 UIScrollView.

In a much more complex senario, you could create your own custom view that contains a UIScrollView that you would place all the images in.

这篇关于从网站获取图像列表并显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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