封装一个 图片异步下载类
来源:互联网 发布:网络教育是全日制吗 编辑:程序博客网 时间:2024/05/21 17:01
这个方法就是根据网络下载get 异步方法来实现的
get异步代理方法
1.创建一个网址对象 对象是根据字符串来创立的
<span style="font-size:24px;">NSURL *url = [NSURL URLWithString:str];</span>2.根据网址对象 创建一个可变请求
<span style="font-size:24px;">NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30];</span>
[request setHTTPMethod:@"Get"];
3.创建链接(异步代理方法)
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
[self.connection start];
ImageDownLoader.h
#import <Foundation/Foundation.h>
/*
本类可以异步加载图片
*/
//创建一个协议
@protocol ImageDownLoaderDelegate <NSObject>
//请求成功
- (void)imageDownLoaderSucceedWithData:(NSData *)data;
//请求失败
- (void)imageDownLoaderFailedWithData:(NSError *)error;
@end
@interface ImageDownLoader : NSObject
// 链接
@property (nonatomic,retain)NSURLConnection *connection;
// data
@property (nonatomic,retain)NSMutableData *data;
// 声明代理对象
@property (nonatomic,assign)id <ImageDownLoaderDelegate> delegate;
//初始化方法(有需要写 请求中 可能遇到的参数 -- 网址)
- (instancetype)initWithUrl:(NSString *)url delegate:(id <ImageDownLoaderDelegate>) delegate;
//开始方法
- (void)imageDownLoaderStart;
//终止方法
- (void)imageDownLoaderCancel;
@end
ImageDownLoader.m
#import "ImageDownLoader.h"
@interface ImageDownLoader ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
@end
//图片一般get请求
@implementation ImageDownLoader
- (void)dealloc
{
[_connection release];
[_data release];
[super dealloc];
}
- (instancetype)initWithUrl:(NSString *)url delegate:(id<ImageDownLoaderDelegate>)delegate
{
self = [super init];
if (self) {
//设置代理
self.delegate = delegate;
//创建网址对象
NSURL *uurl = [NSURL URLWithString:url];
//创建请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:uurl cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30];
[request setHTTPMethod:@"Get"];
//创建链接(异步代理方法)
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
[self.connection start];
}
return self;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.data = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//图片请求完成
if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownLoaderSucceedWithData:)]) {
//代理调用
[_delegate imageDownLoaderSucceedWithData:self.data];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownLoaderFailedWithData:)]) {
[_delegate imageDownLoaderFailedWithData:error];
}
}
//开始方法
- (void)imageDownLoaderStart
{
[self.connection start];
}
//终止方法
- (void)imageDownLoaderCancel
{
[self.connection cancel];
}
@end
在RootViewController.m上
#import "RootViewController.h"
#import "ImageDownLoader.h"
#define kImageUrl @"http://img3.douban.com/view/event_poster/median/public/10f53a2ad8b38c5.jpg"
@interface RootViewController ()<ImageDownLoaderDelegate>
@property (nonatomic ,retain)UIImageView *imageView;
@property (nonatomic ,retain)ImageDownLoader *imageDownLoader;
@end
@implementation RootViewController
- (void)dealloc
{
//当页面被销毁时 终止请求
[_imageDownLoader imageDownLoaderCancel];
[_imageDownLoader release];
[_imageView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:self.imageView];
[_imageView release];
//创建请求对象
self.imageDownLoader = [[ImageDownLoader alloc] initWithUrl:kImageUrl delegate:self];
//开始请求
//[imageDownLoader imageDownLoaderStart];
[_imageDownLoader release];
}
//请求成功
- (void)imageDownLoaderSucceedWithData:(NSData *)data
{
self.imageView.image = [UIImage imageWithData:data];
NSLog(@"%@",data);
}
//请求失败
- (void)imageDownLoaderFailedWithData:(NSError *)error
{
}
- 封装一个 图片异步下载类
- 图片封装下载----异步代理下载
- 图片封装下载----异步block传值
- 一个关于异步下载的封装
- 封装一个异步下载的工具包
- 封装的一个用来下载图片的类
- 封装一个工具类,搞定图片批量下载
- PHP--封装一个下载图片的方法
- objective-c一个简单的异步下载的封装
- 44.封装类实现异步加载图片
- HTTP_WebImageView类(自定义封装下载图片类)
- listview异步下载图片
- 异步下载图片
- 异步下载图片
- AsyncTask异步下载图片
- iOS 异步下载图片
- CGD 异步下载图片
- AsyncImageView 异步下载图片
- 领域驱动设计和开发实战
- 推箱子问题
- [iOS]MVVM-框架介绍
- 电子或通信领域当前的主流及其社会需求调查报告
- Win8.1怎么安装Microsoft .NET Framework 3.5?(sqlserver 2012 安装在win8.1)
- 封装一个 图片异步下载类
- iOS开发之浅谈MVVM的架构设计与团队协作
- 移动小球 欢迎提出更佳方案(要完整代码)
- 单链表逆置算法图解
- python核心编程-诊断函数
- 微信蓝牙demo服务器官方代码操作——配置接口配置
- C++:基于LL(1)方法的语法分析程序-3
- LInux Shell编程初探
- JavaScript-DOM编程