封装的一个用来下载图片的类
2014-03-15 11:30
295 查看
本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6658347
imagedownloadinterface2010
// ImageDownloader
// Created by Tracy E on 10-9-5.
// Copyright 2010 tracy.cpp@gmail.com. All rights reserved.
//
@protocol ImageDownloaderDelegate;
@interface ImageDownloader : NSObject
{
NSString *imageURL;
NSMutableData *activeDownload;
NSURLConnection *imageConnection;
id <ImageDownloaderDelegate> delegate;
}
@property (nonatomic,retain) NSString *imageURL;
@property (nonatomic,assign) id <ImageDownloaderDelegate> delegate;
@property (nonatomic,retain) NSMutableData *activeDownload;
@property (nonatomic,retain) NSURLConnection *imageConnection;
- (void)startDownload;
- (void)cancelDownload;
@end
@protocol ImageDownloaderDelegate
- (void)downloader:(ImageDownloader *)downloader DidFinishDownloadImage:(UIImage *)image;
@end
//
// ImageDownloader
// Created by Tracy E on 10-9-5.
// Copyright 2010 tracy.cpp@gmail.com. All rights reserved.
//
#import "ImageDownloader.h"
@implementation ImageDownloader
@synthesize imageURL;
@synthesize delegate;
@synthesize activeDownload;
@synthesize imageConnection;
#pragma mark
- (void)dealloc
{
[activeDownload release];
[imageConnection cancel];
[imageConnection release];
[super dealloc];
}
- (void)startDownload
{
self.activeDownload = [NSMutableData data];
// alloc+init and start an NSURLConnection; release on completion/failure
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:imageURL]] delegate:self];
self.imageConnection = conn;
[conn release];
}
- (void)cancelDownload
{
[self.imageConnection cancel];
self.imageConnection =nil;
self.activeDownload =nil;
}
#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.activeDownload appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// Clear the activeDownload property to allow later attempts
self.activeDownload =nil;
// Release the connection now that it's finished
self.imageConnection =nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Set appIcon and clear temporary data/image
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
// call our delegate and tell it that our icon is ready for display
[delegate downloader:self DidFinishDownloadImage:image]
self.activeDownload =nil;
[image release];
// Release the connection now that it's finished
self.imageConnection =nil;
}
@end
//use the imageDownloader
- (void)viewDidLoad{
//....
ImageDownloader *downloader = [[ImageDownloader alloc] init];
[downloader setDelegate:self];
downloader.imageURL = imageURLString;
[downloader startDownload];
[downloader release];
//....
}
- (void)downloader:(ImageDownloader *)downloader DidFinishDownloadImage:(UIImage *)image{
//image did finish download, receive the image here
if (downloader !=nil) {
[imageView setImage:image];
}
}
imagedownloadinterface2010
// ImageDownloader
// Created by Tracy E on 10-9-5.
// Copyright 2010 tracy.cpp@gmail.com. All rights reserved.
//
@protocol ImageDownloaderDelegate;
@interface ImageDownloader : NSObject
{
NSString *imageURL;
NSMutableData *activeDownload;
NSURLConnection *imageConnection;
id <ImageDownloaderDelegate> delegate;
}
@property (nonatomic,retain) NSString *imageURL;
@property (nonatomic,assign) id <ImageDownloaderDelegate> delegate;
@property (nonatomic,retain) NSMutableData *activeDownload;
@property (nonatomic,retain) NSURLConnection *imageConnection;
- (void)startDownload;
- (void)cancelDownload;
@end
@protocol ImageDownloaderDelegate
- (void)downloader:(ImageDownloader *)downloader DidFinishDownloadImage:(UIImage *)image;
@end
//
// ImageDownloader
// Created by Tracy E on 10-9-5.
// Copyright 2010 tracy.cpp@gmail.com. All rights reserved.
//
#import "ImageDownloader.h"
@implementation ImageDownloader
@synthesize imageURL;
@synthesize delegate;
@synthesize activeDownload;
@synthesize imageConnection;
#pragma mark
- (void)dealloc
{
[activeDownload release];
[imageConnection cancel];
[imageConnection release];
[super dealloc];
}
- (void)startDownload
{
self.activeDownload = [NSMutableData data];
// alloc+init and start an NSURLConnection; release on completion/failure
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:imageURL]] delegate:self];
self.imageConnection = conn;
[conn release];
}
- (void)cancelDownload
{
[self.imageConnection cancel];
self.imageConnection =nil;
self.activeDownload =nil;
}
#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.activeDownload appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// Clear the activeDownload property to allow later attempts
self.activeDownload =nil;
// Release the connection now that it's finished
self.imageConnection =nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Set appIcon and clear temporary data/image
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
// call our delegate and tell it that our icon is ready for display
[delegate downloader:self DidFinishDownloadImage:image]
self.activeDownload =nil;
[image release];
// Release the connection now that it's finished
self.imageConnection =nil;
}
@end
//use the imageDownloader
- (void)viewDidLoad{
//....
ImageDownloader *downloader = [[ImageDownloader alloc] init];
[downloader setDelegate:self];
downloader.imageURL = imageURLString;
[downloader startDownload];
[downloader release];
//....
}
- (void)downloader:(ImageDownloader *)downloader DidFinishDownloadImage:(UIImage *)image{
//image did finish download, receive the image here
if (downloader !=nil) {
[imageView setImage:image];
}
}
相关文章推荐
- 封装的一个用来下载图片的类
- 封装的一个用来下载图片的类
- 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选
- 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选
- 下载图片封装一个工具类,搞定图片批量下载
- 下载图片封装一个工具类,搞定图片批量下载
- PHP--封装一个下载图片的方法
- 封装一个 图片异步下载类
- 官网下载qt-opensource-windows-x86-mingw482_opengl-5.3.1.exe。封装好了Qt libraries、Qt Creator。只需要这一个可执行程序就好了。
- 图片封装下载——同步下载
- 一个小白的起点 android 主线程不能下载图片到imageview
- 用java下载一个网页图片
- 昨天晚上写了个 Python 程序,下载了一个福利网站 5000 来张妹子图片
- AFNetWorking的简单封装 图片音频 视频下载 上传 请求数据
- ,避免造成混乱。 设想1:拿ListView为例,如果ListView的ItemView复用机制,所有的ItemView复用同一个,如果在多线程下载图片的情况下,可能最终只有最后一个View显示图片
- 一个自己的图片下载库--NeacyImageLoader
- 一个下载图片的例子,为以后铺路。
- AsyncTask 的简单使用,给定一个uri地址下载图片,并显示进度条
- 官网下载qt-opensource-windows-x86-mingw482_opengl-5.3.1.exe。封装好了Qt libraries、Qt Creator。只需要这一个可执行程序就好了。
- COCOS2D-X之使用CURL下载图片的一个简单Demo