//
// ImageDownloader.h
// NSOperationTest
//
// Created by ydc on 11-10-29.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
// #import <Foundation/Foundation.h> @protocol imageDownloaderDelegate; @interface ImageDownloader : NSOperation
{
NSURLRequest* _request; NSURLConnection* _connection; NSMutableData* _data; BOOL _isFinished; BOOL _cancelled;
} - (id)initWithURLString:(NSString *)url; @property(readonly) NSData *data;
@property(nonatomic, assign) id<imageDownloaderDelegate> delegate;
@property(nonatomic, strong) NSObject *delPara;
@property(nonatomic, strong) NSRecursiveLock *cancelledLock; @end @protocol imageDownloaderDelegate @optional //图片下载完毕的托付
- (void)imageDidFinished:(UIImage *)image para:(NSObject *)obj; @end
//
// ImageDownloader.m
// NSOperationTest
//
// Created by ydc on 11-10-29.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
// #import "ImageDownloader.h" @implementation ImageDownloader - (id)initWithURLString:(NSString *)url
{ self = [self init];
if (self) {
assert(url != nil);
_request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
_data = [NSMutableData data];
}
return self;
} - (void)start { if (![self isCancelled]) { [NSThread sleepForTimeInterval:3];
// 以异步方式处理事件,并设置代理 _connection=[NSURLConnection connectionWithRequest:_request delegate:self]; while(_connection != nil) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } } } #pragma mark NSURLConnection delegate Method // 接收到数据(增量)时 - (void)connection:(NSURLConnection*)connection
didReceiveData:(NSData*)data
{
[_data appendData:data];
if (self.delegate != nil){
UIImage *img = [[UIImage alloc] initWithData:self.data] ;
[_delegate imageDidFinished:img para:self.delPara];
}
} - (void)connectionDidFinishLoading:(NSURLConnection*)connection {
if (self.delegate != nil)
{
UIImage *img = [[UIImage alloc] initWithData:self.data] ;
[_delegate imageDidFinished:img para:self.delPara];
}
_connection=nil;
} -(void)connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
{
_connection=nil;
}
- (void)cancelOnRequestThread
{
[[self cancelledLock] lock];
if ([self isCancelled]) {
[[self cancelledLock] unlock];
return;
}
[self willChangeValueForKey:@"isCancelled"];
_cancelled = YES;
[self didChangeValueForKey:@"isCancelled"]; [[self cancelledLock] unlock];
}
- (BOOL)isCancelled
{
BOOL result; [[self cancelledLock] lock];
result = _cancelled;
[[self cancelledLock] unlock]; return result;
}
-(BOOL)isConcurrent
{
return YES;
}
- (BOOL)isExecuting
{
return _connection == nil;
}
- (BOOL)isFinished
{
return _connection == nil;
} @end

@property (strong,
nonatomic)NSOperationQueue *queue;

 NSString *newUrl = [NSString stringWithFormat:@"http://static2.dmcdn.net/static/video/666/645/43546666:jpeg_preview_source.jpg?%d",(int)[NSDate timeIntervalSinceReferenceDate]];
ImageDownloader *imageDownloader = [[ImageDownloader alloc]initWithURLString:newUrl];
imageDownloader.delegate = self;
[self.queue addOperation:imageDownloader];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
}];

下载文件断点续传

NSMutableURLRequest *fileRequest = [NSMutableURLRequest requestWithURL:self.downloadURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:kFileDefaultTimeout];

假设文件存在则 断点续传

_startFileSize = [[fm attributesOfItemAtPath:self.pathToFile error:nil] fileSize];
NSString *range = [NSString stringWithFormat:@"bytes=%lld-", _startFileSize];
[fileRequest setValue:range forHTTPHeaderField:@"Range"];

然后启动下载

  _connection = [[NSURLConnection alloc] initWithRequest:fileRequest
delegate:self
startImmediately:NO];
if (self.connection) {
[self.connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[self willChangeValueForKey:@"isExecuting"];
[self.connection start];
[self didChangeValueForKey:@"isExecuting"];
}

NSOperation 的使用(下载相关) 图片和文件都是能够的 断点续传 图片逐渐显示的更多相关文章

  1. HTML中上传与读取图片或文件(input file)----在路上(25)

    input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...

  2. spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

    spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...

  3. 【Python爬虫程序】抓取MM131美女图片,并将这些图片下载到本地指定文件夹。

    一.项目名称 抓取MM131美女写真图片,并将这些图片下载到本地指定文件夹. 共有6种类型的美女图片: 性感美女 清纯美眉 美女校花 性感车模 旗袍美女 明星写真 抓取后的效果图如下,每个图集是一个独 ...

  4. ios开发多线程四:NSOperation多图下载综合案例

    #import "ViewController.h" #import "XMGAPP.h" @interface ViewController () /** t ...

  5. iOS边练边学--多线程练习的多图片下载 以及 使用第三方框架(SDWebImage)的多图片下载

    一.自己实现多图片下载应该注意的问题 沙盒缓存的问题 程序缓存的问题 cell重复利用显示图片混乱的问题 -- 用户拖拽快,下载图片慢导致的 解决图片混乱引入NSOperation集合的问题 资源下载 ...

  6. js插件---IUpload文件上传插件(包括图片)

    js插件---IUpload文件上传插件(包括图片) 一.总结 一句话总结:上传插件找到真正上传位置的代码,这样就可以知道整个上传插件的逻辑了, 找资料还是github+官方 1.如何在js中找到真正 ...

  7. 前端上传视频、图片、文件等大文件 组件Plupload使用指南

    demo:https://blog.csdn.net/qq_30100043/article/details/78491993 Plupload上传插件中文帮助文档网址:http://www.phpi ...

  8. C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

    我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...

  9. 转: KindEditor 图片空间文件增加删除文件、文件夹功能(ASP语言环境)

    KindEditor 图片上传功能中集成的图片空间文件管理插件可以对已上传图片进行管理,十分便捷,只是没有图片删除功能,仔细研读xieliang分享的经验后,自己动手改造了一下,顺便分享给有同样需求的 ...

随机推荐

  1. 八皇后问题详细分析与解答(递归法解答,c#语言描述)

    八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题.该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或 ...

  2. How to append files to a .tar archive using Apache Commons Compress?(转)

    I created a copy of the tar archive and copied to entire content to it. Then I delete the old tar ar ...

  3. 14.4.4 Configuring the Memory Allocator for InnoDB InnoDB 配置内存分配器

    14.4.4 Configuring the Memory Allocator for InnoDB InnoDB 配置内存分配器 当InnoDB 被开发, 内分配齐 提供了与操作系统和运行库往往缺乏 ...

  4. (76) Clojure: Why would someone learn Clojure? - Quora

    (76) Clojure: Why would someone learn Clojure? - Quora ★ Why would someone learn Clojure?   Edit

  5. Android开发之style属性和提前定义样式

    摘要 Android平台定义的主题样式: android:theme="@android:style/Theme.Dialog" // 将一个Activity显示为对话框模式and ...

  6. VS2008下OpenCV1.0的设置

    原地址:http://hi.baidu.com/caicai_coco/item/0f3b23e1742e3f11595dd825 1.下载安装最新的OpenCV版本,我使用的是OpenCV_1.0. ...

  7. mmap。

    linux mmap 内存映射 mmap() vs read()/write()/lseek() 通过strace统计系统调用的时候,常常能够看到mmap()与mmap2().系统调用mmap()能够 ...

  8. hdu 5071 Chat(模拟)

    题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...

  9. hdu 2642 二维树状数组 单点更新区间查询 模板水题

    Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others) Total Subm ...

  10. C# Dictionary.Add(key,value) 与 Dictionary[key]=value的区别

    1. MSDN上的描述. http://msdn.microsoft.com/zh-cn/library/9tee9ht2(v=VS.85).aspx 通过设置 Dictionary 中不存在的键值, ...