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之手势切换图片示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7881955
《iPhone高级编程:使用Mono Touch和.NET/C#》从基本的概念入手,全面地介绍了基于monotouch使用C#/.net进行iphone应用程序开发。从monotouch和.net开发环境到objective-c和c#开发语言,从基于monotouch进行iphone...
Anyone with programming experience can learn how to write an iPhone app. But if you want to build a great app, there’s a lot more to it than simple coding: you also need to know how design and ...
这是iPhone常用集合类介绍的示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7884218
这是iPhone开发之UIPageControl示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7888322
iPhone开发秘籍:第2版(The iPhone Developer's Cookbook),英文版,清晰PDF版。iphone开发必备佳作,在第一版的基础上进行了全面修订和...iPhone开发的重量级参考书,揭示官方文档未详述的细节,代码示例清晰易懂。
iPhone狂:约会iPhone
这是iPhone之手动切换View示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7879534
iPhone内存清理:删除微信垃圾缓存文件参考.pdf
这是iPhone开发之UIScrollView初步示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7891396
More iPhone 3 Development: Tackling iPhone SDK 3
这是iPhone开发的多线程入门示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7881015
这是iPhone开发之UIWebView示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7888557
这是iPhone开发之重力感应的示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7973661
这是iPhone开发之UITableView入门2示例程序,具体参考: http://blog.csdn.net/ht916/article/details/7897486
这是iPhone开发之JSON解析示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7901785
这是iPhone之NSString常用方法示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7884632
中文介绍:iPhone是一种全新的移动平台,苹果公司为它推出了强大的软件开发工具包iPhone SDK。本书是一部关于iPhone SDK和iPhone开发的基础教程,内容翔实、语言生动。书中结合消费类设备上常见的实例,循序渐进地...
真iPhone玩家秘笈:一份靠谱的苹果手机指南