本文转载至 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. OpenGL.Vertex Array Object (VAO) 【转】

    http://www.cppblog.com/init/archive/2012/02/21/166098.html 一 OpenGL抛弃glEnable(),glColor(),glVertex() ...

  2. sql中用JOIN USING 简化JOIN ON

    Mysql 中联接SQL语句中,ON子句的语法格式为:table1.column_name = table2.column_name. 当模式设计对联接表的列采用了相同的命名样式时,就可以使用 USI ...

  3. WordPress安全检测工具

    Wordpres是一款非常流行cms系统,市面上有相当一部分的博客和站点都是使用wordpress搭建.正因为大多博客都使用wordpress,所以其安全性就显得尤为重要. 我们平时维护一个站点(不仅 ...

  4. 单点登录cas常见问题(四) - ticket有哪些存储方式?

    配置文件ticketRegistry.xml负责配置ticket的存储方式,registry是注冊表,登记薄的意思 经常使用的存储方式包含 1.DefaultTicketRegistry:默认的.存储 ...

  5. 通知传值(NSNotificationCenter)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenVveW91MTMxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  6. 仿新浪首页、主题、详情页,纯html静态页面

    仿新浪首页.主题.详情页.纯html静态页面,下载地址: http://download.csdn.net/detail/sweetsuzyhyf/8085535

  7. LeetCode-Minimum Window Substring -- 窗口问题

    题目描述 Given a string S and a string T, find the minimum window in S which will contain all the charac ...

  8. 基于webmagic的种子网站爬取

    代码地址如下:http://www.demodashi.com/demo/12175.html 1. 概述 因为无聊,闲来没事做,故突发奇想,爬个种子,顺便学习爬虫.本文将介绍使用Spring/Myb ...

  9. Cent OS安装My Sql

    因为公司的需要,所以就自己学习了一下在Linux上安装MySQL,但是翻查了好多博客,没有特别清楚,自己写下来好好总结一下 一.系统环境 CentOS-6.3-i386-bin-DVD1 二.下载My ...

  10. 应用程序之UIWebView的使用

    UIWebView简介 知识点总结 代码实现 一.UIWebView简介 1.是iOS内置的浏览器控件,可以浏览网页.打开文档等2.能够加载html/htm.pdf.docx.txt等格式的文件3.系 ...