2014-02-19 150 views
1

我對iOS中的視網膜與非視網膜圖像有一些疑問。其實我正在下載一些圖像文件,並且它們沒有附加@ 2x後綴。我有幾個問題。視網膜與非視網膜iOS

1 - 首先,下載之後它不在一個文檔庫中,因此@ 2x不會像捆綁的視網膜圖像一樣工作。我的假設是否正確?

2 - 與非視網膜圖像相比,視網膜尺寸縮小了一倍,但如果您將看到視網膜圖像縮放爲2.0,那麼如果我手動將任何圖像縮放到2.0,是否會有任何質量差異?例如我有一個圖像Image1.png並將其轉換爲scale 2.0,只需在UIImageView中添加,而在另一邊我必須使用相同的圖像,但名稱爲[email protected],並且我在UIImageView中添加了Image2。任何質量差異將在Image1中與Image2相比?

這裏是我使用的代碼片段將其轉換爲比例2.0或視網膜,如果圖像是非視網膜。

UIImage *image = [UIImage imageNamed:@"fileName"]; 

UIImage *convertedImage = [UIImage imageWithData:UIImagePNGRepresentation(image) scale:2.]; 

回答

0

下載的圖像放在文檔庫上。您無法使用[UIImage imageNamed:@"filename"]獲取圖像,因爲這些功能只能從包中獲取圖像。我這裏是如何從文檔庫中獲得的圖像:

NSString *bundlePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bundlePath, fileName]]; 

縮放非視網膜成像,以獲得視網膜的大小是不好的做法。我的建議是,你下載的圖像應該是視網膜大小。從中生成非視網膜圖像並將其保存在文檔庫中。

這是示例如何縮放視網膜圖像並將其保存在文檔庫上。要獲得非視網膜大小,您可以手動縮放並保存。這裏的示例代碼:

UIImage *image = [UIImage imageNamed:@"[email protected]"]; 

// set non-retina size from current image 
CGSize size = CGSizeMake(image.size.width/2., image.size.height/2.); 


/** scale the image */ 

UIGraphicsBeginImageContext(size); 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(context, 0.0, size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage); 

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 


/** save scaled image */ 

NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
// save with same name but without suffix "@2x" 
NSString *filePath = [NSString stringWithFormat:@"%@/%@", basePath, @"nonRetinaImage"]; 

@try { 
    [UIImagePNGRepresentation(scaledImage) writeToFile:filePath options:NSAtomicWrite error:nil]; 
} @catch (NSException *exception) { 
    NSLog(@"error while saving non-retina image with exception %@", exception); 
} 
+0

謝謝諾瓦爾,但如果你要縮放視網膜到非視網膜,那麼你將不會得到像視網膜圖像中出現的質量。像100X100在視網膜中顯示爲50X50。我猜這不是縮小的,它是使用某種壓縮技術的?上面的代碼正在做的是縮小它,我想這會降低質量。我在上面提供的代碼中使用了 –

+0

。我得到名爲'yourRetinaImage @ 2x.png'的圖片。我將圖像大小調整爲一半,並用不同的名稱'nonRetinaImage'保存 – nvl

相關問題