本文转载至 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

最新文章

  1. ES5 Objece.creat实现继承
  2. 《OOAD与UML那点儿事》目录索引
  3. Android ANR分析(1)
  4. 开源的DevOps开发工具箱
  5. 在linux上使用"scp"命令拷贝一个目录到另一台服务器的时候报"not a regular file"错误的解决办法
  6. IISExpress配置文件的一个坑
  7. uva 1428
  8. POJ 1422 二分图(最小路径覆盖)
  9. sql操作事务SqlTransHelper类实现
  10. Flink Program Guide (6) -- 窗口 (DataStream API编程指导 -- For Java)
  11. 使用百度语音识别REST API,做全平台语音识别
  12. Java io流的概述
  13. C语言之算法初步(汉诺塔--递归算法)
  14. jdbc-日期格式的转换及代码示例
  15. vue全选与取消全选
  16. html2canvas文字重叠(手机端)
  17. Linux常用命令英文全称与中文解释
  18. sql查询(转)
  19. 在.NET程序中实现HttpServer功能
  20. 幂的运算:X的n次幂

热门文章

  1. DotnetBrowser高级教程-(4)使用MVC框架3-文件上传
  2. ElasticSearch的内存设置
  3. 2017.4.18 putty和fileZilla的使用
  4. [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript
  5. HDU4911:Inversion
  6. git学习——记录每次更新到仓库
  7. 求出数组中所有数字的和&&弹出层效果
  8. WCF 404.3 MIME 映射错误
  9. 关于八数码问题中的状态判重的三种解决方法(编码、hash、<set>)
  10. 【DB2】新建用户