ios开发:如何加载大量图片 相册示例
本文转载至 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.
ios开发:如何加载大量图片 相册示例的更多相关文章
- [IOS 开发] 懒加载 (延迟加载) 的基本方式,好处,代码示例
懒加载的好处: 1> 不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强 2> 每个属性的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合 ...
- iOS开发之加载、滑动翻阅大量图片优化解决方案
本文转载至 http://mobile.51cto.com/iphone-413267.htm 今天分享一下私人相册中,读取加载.滑动翻阅大量图片解决方案,我想强调的是,编程思想无关乎平台限制.我要详 ...
- iOS开发图片加载的内存问题及优化方案
原创作者:Magic-Unique 原文地址:https://github.com/Magic-Unique/HXImage猿吧 - 资源共享论坛: http://www.coderbar.cn 做最 ...
- [deviceone开发]-do_Webview加载JQueryMobile的示例
一.简介 JQueryMobile是JQuery的移动版,不过它并没有像JQuery那么成功.我们只是使用JQueryMobile来展示do_Webview加载第三方js框架.适合所有开发者.二.效果 ...
- ios开发中加载的image无法显示
昨天遇到一个较奇葩的问题,imageName加载的图片显示不出来,网上查了好多资料还是没找到解决的方法: 之前图片是放在项目中SupportingFiles文件下的,怎么加载都能显示图片,于是将图片拿 ...
- iOS - 开发中加载本地word/pdf文档说明
最近项目中要加载一个本地的word/pdf等文件比如<用户隐私政策><用户注册说明>,有两种方法加载 > 用QLPreviewController控制器实现 步骤 : & ...
- iOS 开发笔记-加载/初始化
ViewDidLoad 一般我们会在这里做界面上的初始化操作,比如往view中添加一些子视图.从数据库或者网络加载模型数据装配到子视图中 在自定义控制里 initWithFrame:一般用于添加控件, ...
- iOS开发-UIWebView加载本地和网络数据
UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档,关于浏览网页榜样可以参考UC,手机必备浏览器,至于文档浏览的手机很多图书阅读软件,UIWebView是一个混合体,具体的功能控件内置 ...
- Android开发--异步加载
因为移动端软件开发思维模式或者说是开发的架构其实是不分平台和编程语言的,就拿安卓和IOS来说,他们都是移动前端app开发展示数据和用户交互数据的数据终端,移动架构的几个大模块:UI界面展示.本地数据可 ...
随机推荐
- [置顶]
使用kube-proxy让外部网络访问K8S service的ClusterIP
配置方式 kubernetes版本大于或者等于1.2时,外部网络(即非K8S集群内的网络)访问cluster IP的办法是: 修改master的/etc/kubernetes/proxy,把KUBE_ ...
- spring 配置多数据源 (可行)
可以看到AbstractRoutingDataSource获取数据源之前会先调用determineCurrentLookupKey方法查找当前的lookupKey,这个lookupKey就是数据源标识 ...
- [PWA] Add Push Notifications to a PWA with React in Chrome and on Android
On Android and in Chrome (but not on iOS), it's possible to send push notifications with a PWA. We'l ...
- JAVA Eclipse如何导出APK程序
为了最快的导出程序,直接导出未签名的APK 但是大部分手机不允许安装未经签名的程序 导出签名的项目 设置密码,后面的都可以随便设置 这样最后生成的APK就可以直接双击完成安装 ...
- 在windows上一键编译各种版本的protobuf(2017-12-05修改)
所需工具 : cmake for windows 和 git for windows 原理:protobuf 是google的一个开源项目,其源代码在github上可以下载到,并且源码都采用cm ...
- vue笔记一
一.Vue基本知识点 1.声明式渲染 <div id="app">{{ msg }}</div> <script> var app = new ...
- 【转载】json对象的使用
使用JSON 进行数据传输 一.选择的意义 在异步应用程序中发送和接收信息时,可以选择以纯文本和 XML 作为数据格式.为了更好的使用ajax, 我们将学习一种有用的数据格式 JavaScript O ...
- STL学习笔记(迭代器相关辅助函数)
advance()可令迭代器前进 #include <iterator> void advance(InputIterator& pos,Dist n); 面对Random Acc ...
- Deferred Shader GBuffer 感性认识。。。
- mongo: 删
删除:remove db.CollectionName.remove(查询表达式,选项); 查询表达式:匹配要删除的文档,它是一个json对象 选项:{justOne:true/false},是否只删 ...