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分享的经验后,自己动手改造了一下,顺便分享给有同样需求的 ...
随机推荐
- Linux 双网卡绑定技术
bond技术是在linux2.4以后加入内核. 一般步骤是1.把bonding模块加入内核, 2 编辑要绑定的网卡设置,去除地址设定 3 添加bond设备,设置地址等配置 4 重启网络 5 在交换机 ...
- Java抓取网页数据(原来的页面+Javascript返回数据)
转载请注明出处! 原文链接:http://blog.csdn.net/zgyulongfei/article/details/7909006 有时候因为种种原因,我们须要採集某个站点的数据,但因为不同 ...
- [置顶] 页面缓存,cache,设置缓存过期时间,OutputCache
页面缓存 方法一: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //缓存有数据 if (Cach ...
- ios 正則表達式替换
1. 不可变字符串 (content 是不可变) NSRegularExpression *regularExpression = [NSRegularExpression regularExpr ...
- C#向并口设备发送指令以获取并口设备的状态
using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; usi ...
- 编程算法 - 萨鲁曼的军队(Saruman's Army) 代码(C)
萨鲁曼的军队(Saruman's Army) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 直线上有N个点, 每个点, 其距离为R以内的区域里 ...
- 开发腾讯移动游戏平台SDK Android版Ane扩展 总结
本文记录了在开发 腾讯移动游戏平台SDK(MSDK) Android版Ane扩展 过程中所遇到的问题和相关解决方式 问题一:编译报错:Unable to resolve target 'android ...
- Hulu面试题解答——N位数去除K个数字(解法错误sorry)
给定一个N位数,比如12345,从里面去掉k个数字.得到一个N-k位的数.比如去掉2,4,得到135,去掉1,5.得到234.设计算法.求出全部得到的N-k位数里面最小的那一个. 写的代码例如以下,思 ...
- php获取分类以下的全部子类方法
获取分类以下的全部子类方法: static function getMenuTree($arrCat, $parent_id = 0, $level = 0,$all=True) { static $ ...
- 全面剖析Redis Cluster原理和应用 (转)
1.Redis Cluster总览 1.1 设计原则和初衷 在官方文档Cluster Spec中,作者详细介绍了Redis集群为什么要设计成现在的样子.最核心的目标有三个: 性能:这是Redis赖以生 ...