2011-06-19 55 views
7

我知道如何以編程方式從URL加載我的應用程序的圖像,而不是將它們打包在應用程序中,但是如何處理1x vs 2x問題?如果需要,我可以從外部源提供兩個版本,但在設置UIImage時如何處理?從URL加載時應該如何處理視網膜/正常圖像?

+0

你嘗試只是引用http://example.com/image.png但在該位置處的image&[email protected]文件? – Luke

回答

7

我敢肯定,您無法以自動方式遠程加載@ 2個圖像文件。你將不得不測試Retina顯示屏,然後獲得相應的圖像(S),像這樣:

UIImage *image; 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){ 
    // @2x 
    NSURL *imageURL = [NSURL URLWithString:@"http://www.example.com/images/[email protected]"]; 
    NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; 
    image = [UIImage imageWithData:imageData]; 
} else { 
    // @1x 
    NSURL *imageURL = [NSURL URLWithString:@"http://www.example.com/images/yourImage.png"]; 
    NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; 
    image = [UIImage imageWithData:imageData]; 
} 
UIImageView *yourImageView = [[UIImageView alloc] initWithImage:image]; 
+4

不應該像''[yourImageView setContentScaleFactor:[[UIScreen mainScreen] scale]''''將'yourImageView'的'contentScaleFactor'設置爲[[UIScreen mainScreen] scale]'' – Ali

相關問題