2012-12-20 49 views
0

你好:我在我的應用程序中有一個照片上傳器,可以讓玩家上傳他們自己的照片供我的應用程序使用。我裁剪和調整每個上傳的圖像以創建兩個圖像:一個X-X像素圖像(用於非視網膜顯示)和一個2X像素圖像(用於視網膜顯示)。以編程方式將視網膜和非視網膜圖像通過writeToFile添加到本地商店?

我然後通過[photoDataNonRetina writeToFile:pathNonRetina atomically:YES][photoDataRetina writeToFile:pathRetina atomically:YES]其中photoDataNonRetina和photoDataRetina是NSData的對象,併爲每個圖像文件名分別爲photo.png[email protected]節省圖像本地玩家的私人文件目錄。

我應該如何隨後從本地Private Documents目錄中檢索我的圖像,以便根據設備是否具有視網膜顯示來檢索合適的圖像?現在,我猜想做下面的內容類似:

NSString *path = [[self pathForPlayer:player] stringByAppendingPathComponent:@"photo.png"]; 
return [UIImage imageWithContentsOfFile:path]; 

不同的是僅似乎加載非視網膜圖像?

+1

爲什麼你不試試 –

+0

你的問題是什麼? – 2012-12-20 19:14:24

+1

爲什麼要創建兩種尺寸的圖像?給定的設備只需要兩種尺寸中的一種。只需創建當前設備所需的大小(X或2X)。 – rmaddy

回答

0

試試這個:

if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]   && 
([UIScreen mainScreen].scale == 2.0)) { 
    // Retina display 
} else { 
    // non-Retina display 
} 
+0

檢查主屏幕是否響應'displayLinkWithTarget:selector:'方法的目的是什麼?這與這個問題有什麼關係? – rmaddy

0

也許這?

if ([[UIScreen mainScreen].scale > 1) { 
    NSString *path = [[self pathForPlayer:player] stringByAppendingPathComponent:@"[email protected]"]; 
    UIImage * image = [UIImage imageWithContentsOfFile:path]; 
    UIImage * retinaImage = [UIImage imageWithCGImage:[image CGImage] scale:2 orientation:[image imageOrientation]]; 
    return retinaImage; 
} else { 
    NSString *path = [[self pathForPlayer:player] stringByAppendingPathComponent:@"photo.png"]; 
    return [UIImage imageWithContentsOfFile:path]; 
} 
0

只需從文件名中刪除擴展名即可。

NSString *path = [[self pathForPlayer:player] stringByAppendingPathComponent:@"photo"]; 
return [UIImage imageWithContentsOfFile:path]; 
+0

這看起來很有前途!我必須嘗試一下。我會做更新。 –

相關問題