iphone:使用NSURLConnection下载网络图片
2012-03-31 17:54
399 查看
from: http://www.iblue.cc/2011/04/使用nsurlconnection下载网络图片/
这是一个很基本的demo,直接看代码,你应该可以看得懂。
IconDownloader.h
===============================
@interface IconDownloader :
NSObject
{
NSString *imageURLString;
NSMutableData *activeDownload;
NSURLConnection *imageConnection;
}
@property (nonatomic,
retain) NSString *imageURLString;
@property (nonatomic,
retain) NSMutableData *activeDownload;
@property (nonatomic,
retain) NSURLConnection *imageConnection;
- (void)startDownload;
- (void)cancelDownload;
@end
IconDownloader.m
=====================================
#import
"IconDownloader.h"
@implementation IconDownloader
@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:imageURLString]]
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];
self.activeDownload =
nil;
[image
release];
// Release the connection now that it's finished
self.imageConnection =
nil;
}
@end
这是一个很基本的demo,直接看代码,你应该可以看得懂。
IconDownloader.h
===============================
@interface IconDownloader :
NSObject
{
NSString *imageURLString;
NSMutableData *activeDownload;
NSURLConnection *imageConnection;
}
@property (nonatomic,
retain) NSString *imageURLString;
@property (nonatomic,
retain) NSMutableData *activeDownload;
@property (nonatomic,
retain) NSURLConnection *imageConnection;
- (void)startDownload;
- (void)cancelDownload;
@end
IconDownloader.m
=====================================
#import
"IconDownloader.h"
@implementation IconDownloader
@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:imageURLString]]
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];
self.activeDownload =
nil;
[image
release];
// Release the connection now that it's finished
self.imageConnection =
nil;
}
@end
相关文章推荐
- iphone:使用NSURLConnection下载网络图片
- 使用okhttp进行图片下载,上传,get,post网络请求
- 使用Okhttp网络请求下载图片到指定文件夹
- 安卓Service组件使用系列3:使用IntentService下载网络图片
- java网络编程____使用url下载远程文件(图片,软件等)
- iPhone网络开发之如何使用NSURLConnection
- 0314-使用HttpURLConnection下载网络图片并显示
- 学习笔记之——简单使用facebook/fresco库网络下载图片
- Android异步下载网络图片&android解析xml文件的方式&使用Adapter为ListView提供数据
- iPhone网络开发之如何使用NSURLConnection(一)
- 使用HttpClient下载网络图片
- Android UI-开源框架ImageLoader完美使用+listview加载网络图片与下载
- Volley的初级使用(从网络下载图片)
- Node.js 使用爬虫批量下载网络图片到本地
- swift 使用Kingfisher进行网络图片的下载
- 安卓多线程编程系列2:异步任务的使用之使用异步任务带有进度的横向滚动条下载网络图片
- 使用Service服务下载网络图片并写入sdCard
- iPhone网络开发之如何使用NSURLConnection(一)
- Android 下载显示网络图片 实现直接使用类似setimage的方法来完成
- 使用HttpURLConnection从网络中下载图片