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分享的经验后,自己动手改造了一下,顺便分享给有同样需求的 ...
随机推荐
- maven项目配置Project Facets时further configuration available不出来问题
如果下边的 further configuration available不出来 把Dynamic web module 去掉勾选,应用与项目,然后再点开项目的properties,再选中Dynami ...
- Android installed app, never used, cannot receiver BroadcastReceiver
官方文档是这么写的:(http://developer.android.com/about/versions/android-3.1.html#launchcontrols) Launch contr ...
- Leetcode-Database-176-Second Highest Salary-Easy(转)
leetcode地址:https://oj.leetcode.com/problems/second-highest-salary/ 这个问题很有趣,是要求我们写个sql来查询Employee表里第二 ...
- SilkTest高级进阶系列6-motif tag
看SilkTest代码的时候不小心看到winclass.inc里面的一些类申明使用的是motif tag,例如: [-] winclass MessageBoxClass : DialogBox [ ...
- WPF 设置WebBrowser控件不弹脚本错误提示框
using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.N ...
- 《Python学习手册》读书笔记
之前为了编写一个svm分词的程序而简单学了下Python,觉得Python很好用,想深入并系统学习一下,了解一些机制,因此开始阅读<Python学习手册(第三版)>.如果只是想快速入门,我 ...
- WinCE隐藏显示任务栏,当任务栏隐藏时将其显示,当任务栏显示时将其隐藏(FindWindow,ShowWindow,IsWindowVisible),
HANDLE hWndTaskBar = ::FindWindow(TEXT("HHTaskBar"), NULL); if(::IsWindowVisible(hWndTask ...
- OpenStack25
OpenStack(25) API 前端服务 每个 OpenStack 组件可能包含若干子服务,其中必定有一个 API 服务负责接收客户请求. 以 Nova 为例,nova-api 作为 Nova 组 ...
- 在Windows下搭建React Native Android开发环境
widows版本: win7 64位 专业版 1. 安装jdk.(我用的jdk7) 注意选择x86还是x64版本, 添加到系统PATH环境变量 2. 准备好android sdk 这个不多说,同时推荐 ...
- python语言学习3 ——第一个python程序
输入exit即退出,这样写的缺点是没有保存已经写的代码,下次需要重新写