本文转载至 http://www.cnblogs.com/xiongqiangcs/archive/2013/06/13/3134486.html

 

1. Create a NSOperationQueue to handle background downloading.

NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];

2. Now create an NSInvocationOperation for each image that you want to download and add it to operationQueue.

int noOfImages = 500;
for(int i = 0; i noOfImages; i++)
{
NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadDataAsychn:) object:i];
[queue addOperation:invocationOperation];
[operation release];
}

In this code, we have created an operation calling loadDataAsynchn: method which is to be implemented and added to queue. After adding created operation to the queue, it will do the rest of the downloading work for your app. You can add more operations to the same queue to be performed later.

Now we will implement loadDataAsychn method.

3. Implement loadDataAsychn method for loading data from the given URL, creating an image and setting it to desired placeholder.

-(void)loadDataAsychn:(NSString*)imageNo
{
NSString *imgStr = [NSString stringWithFormat:@"http://192.168.0.163/Test/images/%@.jpg",imageNo];
NSData *imageData=[[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:imgStr]];
UIImage *image=[[UIImage alloc]initWithData:imageData];
[imgDict setValue:image forKey:@"image"];
[imgDict setValue:imageNo forKey:@"imageNo"];
[self performSelectorOnMainThread:@selector(showImg:) withObject:imgDict waitUntilDone:NO];
}

The loadDataAsychn method is used to download the information from a given URL and creating an image with downloaded data. As we all know that none of the UI related methods can be performed on background thread. So, in loadDataAsychn method, we call showImage method on the main thread to display images.

4. We will now implement showImage which is responsible for updating UI.

(void)showImage:(NSMutableDictionary *)Dict
{
int i = [[Dict objectForKey:@"imageNo"] intValue];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(320*(i-1), 0, 320, 460)];
[imgView setImage:[Dict objectForKey:@"image"]];
[myScrollView addSubview:imgView];
}

In this method, we create an UIImageView for displaying image, setting image on it and adding it to scroll-view dynamically.

官方的相册程序示例  http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010080-Intro-DontLinkElementID_2

ios开发:如何加载大量图片 相册示例的更多相关文章

  1. [IOS 开发] 懒加载 (延迟加载) 的基本方式,好处,代码示例

    懒加载的好处: 1> 不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强 2> 每个属性的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合 ...

  2. iOS开发之加载、滑动翻阅大量图片优化解决方案

    本文转载至 http://mobile.51cto.com/iphone-413267.htm 今天分享一下私人相册中,读取加载.滑动翻阅大量图片解决方案,我想强调的是,编程思想无关乎平台限制.我要详 ...

  3. iOS开发图片加载的内存问题及优化方案

    原创作者:Magic-Unique 原文地址:https://github.com/Magic-Unique/HXImage猿吧 - 资源共享论坛: http://www.coderbar.cn 做最 ...

  4. [deviceone开发]-do_Webview加载JQueryMobile的示例

    一.简介 JQueryMobile是JQuery的移动版,不过它并没有像JQuery那么成功.我们只是使用JQueryMobile来展示do_Webview加载第三方js框架.适合所有开发者.二.效果 ...

  5. ios开发中加载的image无法显示

    昨天遇到一个较奇葩的问题,imageName加载的图片显示不出来,网上查了好多资料还是没找到解决的方法: 之前图片是放在项目中SupportingFiles文件下的,怎么加载都能显示图片,于是将图片拿 ...

  6. iOS - 开发中加载本地word/pdf文档说明

    最近项目中要加载一个本地的word/pdf等文件比如<用户隐私政策><用户注册说明>,有两种方法加载 > 用QLPreviewController控制器实现 步骤 : & ...

  7. iOS 开发笔记-加载/初始化

    ViewDidLoad 一般我们会在这里做界面上的初始化操作,比如往view中添加一些子视图.从数据库或者网络加载模型数据装配到子视图中 在自定义控制里 initWithFrame:一般用于添加控件, ...

  8. iOS开发-UIWebView加载本地和网络数据

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档,关于浏览网页榜样可以参考UC,手机必备浏览器,至于文档浏览的手机很多图书阅读软件,UIWebView是一个混合体,具体的功能控件内置 ...

  9. Android开发--异步加载

    因为移动端软件开发思维模式或者说是开发的架构其实是不分平台和编程语言的,就拿安卓和IOS来说,他们都是移动前端app开发展示数据和用户交互数据的数据终端,移动架构的几个大模块:UI界面展示.本地数据可 ...

随机推荐

  1. WEB服务器、应用程序服务器、HTTP服务器区别 【转】

    WEB服务器.应用程序服务器.HTTP服务器有何区别?IIS.Apache.Tomcat.Weblogic.WebSphere都各属于哪种服务器,这些问题困惑了很久,今天终于梳理清楚了: Web服务器 ...

  2. phpMyAdmin搭建及管理多台数据库服务器

    phpMyAdmin搭建及管理多台数据库服务器 环境说明: 系统版本    CentOS 6.9 x86_64         软件版本    nginx-1.12.2        php-5.5. ...

  3. 【共享单车】—— React后台管理系统开发手记:UI菜单各个组件使用(Andt UI组件)

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  4. ajty

    ] .Tag //; ].Columns["NodeID"].ColumnName ; ].Tag)["NodeID"]; //(DataRow)row[&qu ...

  5. EffectiveJava(7)避免使用终结方法

    避免使用终结方法 1.使用终结方法会导致行为不稳定,性能降低,以及可移植性的问题.(终结线程的优先级过低) 终结方法不能保证被及时的执行(从一个对象变得不可到达开始,到中介方法被执行,所花费的时间是任 ...

  6. mysql ubuntu 开启3306端口,设置远程访问

    远程登陆数据库的时候出现了下面出错信息 :ERROR 2003 ( HY000 ) : Can 't connect to MySQL server on ' xxx.xxx.xxx.xxx ',经过 ...

  7. Android学习(十三) BroadcastReceiver组件(广播)

    一.Broadcast(广播) 是一种广泛应用在应用程序之间传输信息的机制. 二.Broadcast(广播接收器) 是对发送出来的广播进行过滤接收并响应的一类组件,它就是用来接收来自系统和应用中的广播 ...

  8. Linux非阻塞IO(四)非阻塞IO中connect的实现

    我们为客户端的编写再做一些工作. 这次我们使用非阻塞IO实现connect函数. int connect(int sockfd, const struct sockaddr *addr, sockle ...

  9. Git提交时提示‘The file will have its original line endings in your working directory’

    Git提交时提示'The file will have its original line endings in your working directory' Git出现错误 git add -A ...

  10. ES6 动态计算属性名

    在ES5之前,如果属性名是个变量或者需要动态计算,则只能通过 对象.[变量名] 的方式去访问. <script type="text/javascript"> var ...