iphone:使用NSURLConnection下载网络图片
2012-05-02 13:25
453 查看
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
{
[activeDownloadrelease];
[imageConnectioncancel];
[imageConnectionrelease];
[superdealloc];
}
- (void)startDownload
{
self.activeDownload = [NSMutableDatadata];
// alloc+init and start an NSURLConnection; release on completion/failure
NSURLConnection *conn = [[NSURLConnectionalloc]
initWithRequest:
[NSURLRequestrequestWithURL:
[NSURLURLWithString:imageURLString]]delegate:self];
self.imageConnection = conn;
[conn
release];
}
- (void)cancelDownload
{
[self.imageConnectioncancel];
self.imageConnection =nil;
self.activeDownload =nil;
}
#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)
//每次成功请求到数据后将调下此方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//把每次得到的数据依次放到数组中,这里还可以自己做一些进度条相关的效果
[self.activeDownloadappendData: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
{
[activeDownloadrelease];
[imageConnectioncancel];
[imageConnectionrelease];
[superdealloc];
}
- (void)startDownload
{
self.activeDownload = [NSMutableDatadata];
// alloc+init and start an NSURLConnection; release on completion/failure
NSURLConnection *conn = [[NSURLConnectionalloc]
initWithRequest:
[NSURLRequestrequestWithURL:
[NSURLURLWithString:imageURLString]]delegate:self];
self.imageConnection = conn;
[conn
release];
}
- (void)cancelDownload
{
[self.imageConnectioncancel];
self.imageConnection =nil;
self.activeDownload =nil;
}
#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)
//每次成功请求到数据后将调下此方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//把每次得到的数据依次放到数组中,这里还可以自己做一些进度条相关的效果
[self.activeDownloadappendData: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下载网络图片
- IOS网络篇10之NSURLConnection的使用(同步异步下载getpost)
- ServerU FTP 9.1.0.5完美破解版 使用教程 学校局域网上课上传下载文件用--图片来自网络
- 网络请求----简单框架使用------(百度图片下载实例)
- IPhone 网络开发之如何使用NSURLConnection(一)
- Afinal加载网络图片及下载文件使用方法
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄
- RxJava的使用(线程调度器)---包括从网络下载图片进行显示
- Android异步下载网络图片&android解析xml文件的方式&使用Adapter为ListView提供数据
- 安卓Service组件使用系列2:使用Service下载网络图片并存储于sdCard卡上
- iPhone网络开发之如何使用NSURLConnection
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载
- Afinal加载网络图片及下载文件使用方法
- Android使用OkHttp框架下载网络图片
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄
- 使用org.jsoup.Jsoup下载网络中的图片
- 使用接口回调解析数据并下载网络图片
- IPhone 网络开发之如何使用NSURLConnection(一)
- 使用http协议下载网络图片
- python 使用 urllib.urlretrieve()下载网络图片,在本地打开提示文件损坏无法打开