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分享的经验后,自己动手改造了一下,顺便分享给有同样需求的 ...
随机推荐
- POJ 1159 - Palindrome 优化空间LCS
将原串和其逆序串的最长公共子序列求出来为M..那么2*n-M就是所需要加的最少字符..因为求出的M就是指的原串中"潜伏"的最长回文.. 问题转化为求LCS..但是n最大到5000. ...
- ThinkPHP连接数据库出现的错误:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'
最近看了看ThinkPHP.在连接mysql数据库时出现了错误:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'.意思就是没有PDO(PHP数据对象 ...
- [Android]Eclipse的使用
1.取消Eclipse拼写检查 General -> Editors -> Text Editors -> Spelling 取消enable spell checking 前面的勾 ...
- 依赖注入(DI)有助于应用对象之间的解耦,而面向切面编程(AOP)有助于横切关注点与所影响的对象之间的解耦(转good)
依赖注入(DI)有助于应用对象之间的解耦,而面向切面编程(AOP)有助于横切关注点与所影响的对象之间的解耦.所谓横切关注点,即影响应用多处的功能,这些功能各个应用模块都需要,但又不是其主要关注点,常见 ...
- Unix文本处理工具之awk
Unix命令行下输入的命令是文本,输出也都是文本.因此,掌握Unix文本处理工具是很重要的一种能力.awk是Unix常用的文本处理工具中的一种,它是以其发明者(Aho,Weinberger和Kerni ...
- 怎么获取Spring的ApplicationContext
在 WEB 开发中,可能会非常少须要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean, 今天我就遇到了,在这里和大家分享一下, WEB 开发中,怎么获 ...
- Spring MVC的异步模式
高性能的关键:Spring MVC的异步模式 我承认有些标题党了,不过话说这样其实也没错,关于“异步”处理的文章已经不少,代码例子也能找到很多,但我还是打算发表这篇我写了好长一段时间,却一直没发表 ...
- thinkphp3.2
1.安装WAMPServer,到D:\wamp\. 2.下载ThinkPHP3.2.2核心版.解压缩后,放到D:\wamp\www\MyWeb\.打开浏览器,输入网址:http://localhost ...
- hdu 4291 A Short problem(矩阵+取模循环节)
A Short problem Time Limit: 2000/1000 MS (J ...
- CC 3-Palindromes(manacher)
传送门:3-Palindromes 题意:求为回文串且能整除3且不前导0的子串个数. 分析:由 manacher算法O(N)可算出以i为坐标的最长为p[i]回文子串,且Si-k,Si-k+1..... ...