【iOS】記憶體快取
阿新 • • 發佈:2019-02-05
NSString
*paramURLAsString= ;
if ([paramURLAsString length] == 0){
NSLog(@"Nil or empty URL is given");
return;
}
NSURLCache *urlCache = [NSURLCache sharedURLCache];
/* 設定快取的大小為1M*/
[urlCache setMemoryCapacity:1*1024*1024];
//建立一個nsurl NSURL *url = [NSURL URLWithString :paramURLAsString];
//建立一個請求 NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
//從請求中獲取快取輸出 NSCachedURLResponse *response =
[urlCache cachedResponseForRequest:request];
//判斷是否有快取 if (response != nil){
NSLog(@"如果有快取輸出,從快取中獲取資料");
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
}
if ([paramURLAsString length] == 0){
NSLog(@"Nil or empty URL is given");
return;
}
NSURLCache *urlCache = [NSURLCache sharedURLCache];
/* 設定快取的大小為1M*/
[urlCache setMemoryCapacity:1*1024*1024];
//建立一個nsurl NSURL *url = [NSURL URLWithString
//建立一個請求 NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
//從請求中獲取快取輸出 NSCachedURLResponse *response =
[urlCache cachedResponseForRequest:request];
//判斷是否有快取 if (response !=
NSLog(@"如果有快取輸出,從快取中獲取資料");
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
}