2012-07-17 53 views
0

我希望有一些幫助或任何可用於搜索的關鍵字。想要在更改爲其他UIView時保留緩存數據

我的問題是我有一個UIView,稱爲「UpdateViewController.xib」,通過編程加載20個小圖像和文本下方的圖像。 當用戶點擊這些圖片時,它將會更改爲由IB創建的下一個視圖,稱爲「imageSumVuew.xib」,並且我有一個鏈接回到UpdateViewController的按鈕。

#import "imageSumView.h" // next view that i wanna load// 
// the transition to next view 
imageSumView *nextView = [[imageSumView alloc]init]; 
self.modalPresentationStyle = UIModalTransitionStyleCrossDissolve; 
[self presentViewController:nextView animated:YES completion:NULL]; 
[nextView release]; 
在新思維

我有與此類似,其回到這個視圖代碼

#import "UpdateViewController.h" // old that i wanna load back// 
    // the transition to old view 
    UpdateViewController *oldView = [[UpdateViewController alloc]init]; 
    self.modalPresentationStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentViewController:oldView animated:YES completion:NULL]; 
    [oldView release]; 

的問題是,當它做負載回UpdateViewController,我的所有圖片和文字已遍佈重裝再次。

問題是「如何保持UpdateViewController視圖的緩存?」,我不希望用戶重新加載圖像,因爲它們必須在該頁面之間來回多次才能看到他們想要挑選哪個圖像。

想到Instragram,你會看到你的朋友圖片列表,然後你想檢查你的第一個朋友的照片,然後回來看看你的朋友的整體形象,沒有加載和選擇第二個朋友。

回答

0

第一次下載後,將具有唯一標識的圖像數據存儲到臨時目錄。下一次檢查目錄中該用戶標識的圖像,如果存在,則從那裏加載圖像。例如:

 #define TMP NSTemporaryDirectory() 
    NSString *filename=[NSMutableString stringWithFormat:@"userimage_%@",userId]; 

    NSString *uniquePath = [TMP stringByAppendingPathComponent:filename]; 
    NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]] 
    UIImage *image = [[UIImage alloc] initWithData: dataImage]; 
    [UIImageJPEGRepresentation(image, 1.0f) writeToFile: uniquePath atomically: YES]; 
    [image release]; 

要獲得烏爾圖像,你可以這樣做如下:

 
- (NSData *) getImage: (NSString *)userId 
{ 
    NSString *filename=[NSMutableString stringWithFormat:@"userimage_%@,userId]; 
    NSString *uniquePath = [TMP stringByAppendingPathComponent: filename]; 

    if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath]) 
    { 
     return [[NSData alloc] initWithContentsOfFile:uniquePath]; 
    } 
    return nil; 
} 

+0

謝謝。我會嘗試並告訴結果。 – Einzeln 2012-07-18 09:38:02

+0

非常感謝,從你告訴我,我做了更多有關NSFilemanager的研究,並看到了很多例子。所以我決定爲此使用NSCachesDirectory,這是由於Apple使用NSFileDirectory的限制。謝謝!!大拇指 – Einzeln 2012-07-30 15:36:05