异步下载图片(自定义)
来源:互联网 发布:飞鸟淘宝客学习 编辑:程序博客网 时间:2024/06/02 02:44
自定义imageDownLoad类 和 第三方类库SDWebImage
一、第三方类库SDWebImage
导入SDWebImage包后
#import "UIImageView+WebCache.h"
一句话 在imageview对象发消息[imageView sd_setImageWithURL:[NSURLURLWithString:@"string"]placeholderImage:niloptions:SDWebImageRetryFailed];
二、自定义imageDownLoad类
初始化传入下载图片的url字符串
写一个方法实现下载功能
自定义协议 让调用的类遵守 最后在接受数据完后回到主线程的指定方法里 通过代理将值uiimage传过去
.h文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol ImageDownLoadDelegate <NSObject>
-(void)imageDidFinishDownLoadWithImage:(UIImage *)image;
@end
.m文件
@interface ImageDownLoad :NSObject
@property(weak,nonatomic)id<ImageDownLoadDelegate>delegate;
-(instancetype)initWithDownLoadURLString:(NSString *)URLString;
-(void)startDownLoad;
@end
#import "ImageDownLoad.h"
@interface ImageDownLoad ()<NSURLConnectionDataDelegate>
@property(strong,nonatomic)NSURL *url;
@property(strong,nonatomic)NSMutableData *data;
@end
@implementation ImageDownLoad
-(instancetype)initWithDownLoadURLString:(NSString *)URLString
{
self = [superinit];
if (self) {
self.url = [NSURLURLWithString:URLString];
}
returnself;
}
-(void)startDownLoad
{
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:self.url];
[NSURLConnectionconnectionWithRequest:requestdelegate:self];
}
#pragma mark 请求
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.data = [NSMutableDatadata];
}
#pragma mark 请求到数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.dataappendData:data];
}
#pragma mark 请求完成
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *image = [UIImageimageWithData:self.data];
//回到主线程
[selfperformSelectorOnMainThread:@selector(imageDidLoad:)withObject:imagewaitUntilDone:YES];
}
#pragma mark 失败
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
#pragma mark 通过代理方法实现传值
-(void)imageDidLoad:(UIImage *)image
{
if (self.delegate && [self.delegaterespondsToSelector:@selector(imageDidFinishDownLoadWithImage:)]) {
[self.delegateimageDidFinishDownLoadWithImage:image];
}
}
@end
- 异步下载图片(自定义)
- 自定义Gallery异步下载并显示网络图片
- 使用 GCD 自定义 UIImageView 增加异步下载图片的类别
- Android异步下载网络图片(其一)
- Android异步下载网络图片(其二)
- Android异步下载网络图片(其一)
- android异步下载网络图片(其二)
- Android异步下载网络图片(其一)
- Android异步下载网络图片(其二)
- Android异步下载网络图片(其一)
- Android异步下载网络图片(3)
- android异步下载网络图片(其一)
- android异步下载网络图片(其二)
- Android异步下载网络图片(其一)
- Android异步下载网络图片(其二)
- Android异步下载网络图片(其二)
- Android异步下载网络图片(其一)
- Android异步下载网络图片(其二)
- Windows无法连接到System Event Notification Service服务问题解决
- C语言字符指针和字符数组的区别
- 给大学生的18条建议
- 把字符串转换成整数
- QQ第一位产品经理吴宵光:腾讯17年发展史上的14个关键点
- 异步下载图片(自定义)
- 32.文本输入输出
- iOS开发学习第十六课——如何使用苹果帮助文档、NSString
- 花生壳帐号无法登录怎么办
- White spaces are required between publicId and systemId.异常
- 泛型笔记
- leetcod - Merge k Sorted Lists
- 清除垃圾的DOS命令
- 总有点记不住的小东西