1. 程式人生 > >【iOS】記憶體快取

【iOS】記憶體快取

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];
    }