抱歉耽搁了您需要对此进行一些研究:)希望这将有助于通过下载图像尝试的ui,即使在离线时我也会得到它。只是通过这可能对你有帮助:)
在应用程序委托中设置创建缓存
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
//need to add this
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
// set sharedcache
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
//在你的下载视图控制器中
#import "ViewController.h"
@interface ViewController ()<NSURLConnectionDelegate> // confirms to this delegate
{
NSMutableData *imagData;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:@"http://static.adzerk.net/Advertisers/d9a919813dac42adbd0e3106bc19bc04.png"];
NSURLRequest *Req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:Req delegate:self];
if(conn == nil)
{
conn = nil;
[conn cancel];
}
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
if(imagData == nil)
{
imagData = [[NSMutableData alloc]init];
}
// NSURLResponse *resp = [[NSURLResponse alloc]initWithURL:[NSURL URLWithString:@"http://static.adzerk.net/Advertisers/d9a919813dac42adbd0e3106bc19bc04.png"] MIMEType:@"" expectedContentLength:50 textEncodingName:@"image"];
// NSLog(@"%@",connection.description);
// NSLog(@"%@",resp.description);
//
// NSCachedURLResponse *chacheResp = [[NSCachedURLResponse alloc]initWithResponse:resp data:data];
//
[imagData appendData:data];
UIImage *aimage=[[UIImage alloc]initWithData:imagData];
self.aview.image = aimage;
}
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
NSMutableDictionary *userInfo = [[cachedResponse userInfo] mutableCopy];
NSMutableData *mutData = [[cachedResponse data] mutableCopy];
NSURLCacheStoragePolicy storagePolicy = NSURLCacheStorageAllowedInMemoryOnly;
return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response]
data:mutData
userInfo:userInfo
storagePolicy:storagePolicy];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
connection = nil;
[connection cancel];
}
并通过 AdamG 提供的链接