2011-03-19 19 views
2

我一直在試圖從使用ASIHTTPRequest的URL加載圖像,但我總是想出一個空白的UIImage。我認爲這可能與iOS自動選擇@ 2x圖像版本或vica相關。如何使用ASIHTTPRequest @ 2x填充UIImage視圖?

[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; 


NSString *url_string = [NSString stringWithFormat:@"http://173.246.100.185/%@", [eventDictionary objectForKey:kEventDescriptionImageURLKey]]; 
NSURL *url = [NSURL URLWithString:url_string]; 
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
[request setDownloadCache:[ASIDownloadCache sharedCache]]; 
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy]; 
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 
[request setSecondsToCache:86400]; 
[request setDelegate:self]; 
[request setCompletionBlock:^{ 
    NSLog(@"Successful Update"); 
    [self makeAssignment]; 
}]; 
[request setFailedBlock:^{ 
    NSError *error = [request error]; 
    NSLog(@"%@", [error localizedDescription]); 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Update Failed" 
                message:[error localizedDescription] 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
}]; 
[request startAsynchronous]; 
NSLog(@"%@", url_string); 

makeAssignment方法如下。

NSString *url_string = [NSString stringWithFormat:@"http://173.246.100.185/%@", [eventDictionary objectForKey:kEventDescriptionImageURLKey]]; 
NSURL *url = [NSURL URLWithString:url_string]; 
downloadedImage = [[UIImage alloc] initWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url]]; 
NSLog(@"%@", downloadedImage); 
NSLog(@"%@", [[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url]); 

沒有我做的,包括在服務器上命名的圖像@ 2倍或提供兩個版本,得到它加載。有任何想法嗎?有沒有人做過這個?當我在本地加載它們時(從包中)我沒有任何問題。

謝謝!

EDIT

這裏的日誌輸出

2011-03-19 11:46:11.088 CLV [82974:207]成功更新

2011-03-19 11:46:12.822 CLV [82974:207] http://173.246.100.185/[email protected]

2011-03-19 11:46:12.844 CLV [82974:207]>

2011-03-19 11:46:12.913 CLV [82974:207]成功更新

2011-03-19 11:46:12.932 CLV [82974:207]

2011-03-19 11:46:12.932 CLV [82974:207] /用戶/ jonathantpage /庫/應用程序支持/ iPhone模擬器/ 4.3 /應用/ A17C0938-D2ED-447C-BD17-94726C5E5A66 /圖書館/緩存/ ASIHTTPRequestCache/PermanentStore/FE05295C8CD7687DC7A505C9070B6FC7.png

回答

0

它不會自動@ 2X的事情 - 如果系統無法找到一個帶有'@ 2x'的圖像附加到它上面,它將簡單地使用原始圖像並將其縮放。如果你想驗證,只需使用非視網膜顯示模式在模擬器上運行應用程序。

如果您在模擬器上運行您的應用程序,您實際上可以使用查找器瀏覽緩存目錄,以驗證您的圖像是否如您所期望的那樣存在。在您的用戶目錄中,您將轉至~/Library/Application Support/iPhone Simulator/[Your SDK version]/Applications/[Your app]/以訪問您的應用沙箱,並在那裏形成該應用的庫/緩存目錄。

此外,您記錄緩存路徑:它是什麼出來的?你確定這是一條有效的路徑嗎?再次,如果你在模擬器上,你可以通過嘗試在終端/另一個應用程序中打開路徑來快速驗證這一點。

你的代碼中至少看起來沒有什麼特別的錯,所以感覺就像本地文件路徑發生了什麼事情。也許你可以將你得到的NSLog發佈到控制檯。

+0

我檢查了本地文件路徑,它在那裏(和正確的視網膜大小的圖像)。 – 2011-03-19 16:48:14

0

事實證明,一切都很好。什麼是錯誤的是,完成塊正在執行UIImageView後執行。把它放在makeAssignment方法中一切正常。以前我只是在那裏填充UIImage,然後將它分配給主線程中的UIImageView。