NSOperation 的使用(下载相关) 图片和文件都是能够的 断点续传 图片逐渐显示
//
// 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 的使用(下载相关) 图片和文件都是能够的 断点续传 图片逐渐显示的更多相关文章
- HTML中上传与读取图片或文件(input file)----在路上(25)
input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...
- spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置
spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...
- 【Python爬虫程序】抓取MM131美女图片,并将这些图片下载到本地指定文件夹。
一.项目名称 抓取MM131美女写真图片,并将这些图片下载到本地指定文件夹. 共有6种类型的美女图片: 性感美女 清纯美眉 美女校花 性感车模 旗袍美女 明星写真 抓取后的效果图如下,每个图集是一个独 ...
- ios开发多线程四:NSOperation多图下载综合案例
#import "ViewController.h" #import "XMGAPP.h" @interface ViewController () /** t ...
- iOS边练边学--多线程练习的多图片下载 以及 使用第三方框架(SDWebImage)的多图片下载
一.自己实现多图片下载应该注意的问题 沙盒缓存的问题 程序缓存的问题 cell重复利用显示图片混乱的问题 -- 用户拖拽快,下载图片慢导致的 解决图片混乱引入NSOperation集合的问题 资源下载 ...
- js插件---IUpload文件上传插件(包括图片)
js插件---IUpload文件上传插件(包括图片) 一.总结 一句话总结:上传插件找到真正上传位置的代码,这样就可以知道整个上传插件的逻辑了, 找资料还是github+官方 1.如何在js中找到真正 ...
- 前端上传视频、图片、文件等大文件 组件Plupload使用指南
demo:https://blog.csdn.net/qq_30100043/article/details/78491993 Plupload上传插件中文帮助文档网址:http://www.phpi ...
- C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)
我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...
- 转: KindEditor 图片空间文件增加删除文件、文件夹功能(ASP语言环境)
KindEditor 图片上传功能中集成的图片空间文件管理插件可以对已上传图片进行管理,十分便捷,只是没有图片删除功能,仔细研读xieliang分享的经验后,自己动手改造了一下,顺便分享给有同样需求的 ...
随机推荐
- 学习android内核 -- 内存管理相关
Android内存管理: 1.当应用程序关闭以后,后台对应的进程并没有真正的退出(处于休眠状态,一般不占用系统CPU的资源),这是为了下次再启动的时候能快速启动. 2.当系统内存不够时,AmS会主动根 ...
- Linux入门基础 #10:命令行文本处理工具
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- 字符串转换为整数”123“->123
字符串转换为整数"123"->123 题目描写叙述: 输入一个由数字组成的字符串.把它转换成整数并输出. 比如:输入字符串"123".输出整数123. 给 ...
- UVA 1386 - Cellular Automaton(循环矩阵)
UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...
- [SVN]两个分支合并
Date:2014-1-1 Summary: 记录一下自己使用SVN时候的操作步骤,先吃鱼,再学钓鱼 Contents: 环境:从同事的branch迁出一份代码,作为自己的分支进行开发,同时同事也在自 ...
- C#实现树的双亲表示法
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHVja3k1MTIyMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- make工具与Makefile文件
make工具与Makefile文件 阅读目录 1. make工具 2. Makefile文件 3. Makefile的简单示例 4. 伪目标 5. Makefile 自动化变量 6. 编译生成多个可执 ...
- haskell,lisp,erlang你们更喜欢哪个?
haskell,lisp,erlang你们更喜欢哪个? haskell,lisp,erlang你们更喜欢哪个?
- BCM wifi分析
一个:载入中wifi驱动模块 在hardware/libhardware_legacy/wifi/wifi.c调用函数 insmod(DRIVER_MODULE_PATH, DRIVER_MODULE ...
- 《WordPress插件开发手冊》文件夹
翻译前言:国内没有关于WordPress插件开发比較具体而且系统的资料 前言 第一章:准备一个本地开发环境 介绍 在你的电脑上安装一个站点server 下载并配置一个本地的WordPress 创建一个 ...