2014-02-16 99 views
0

如果我從服務器下載一些圖像,我應該調整它以支持視網膜設計?下載圖像和視網膜顯示

如果現在我不能使用 - @ 2x,我該如何設置正確的圖像來顯示?

UPDATE:

如果我緩存圖像,我需要下載後和緩存之前調整圖像和高速緩存2個圖像,經常和@ 2倍?

回答

1

你的應用應該檢索視網膜大小的那些圖像。要獲得非視網膜大小,您可以手動縮放並保存。這裏的示例代碼:

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, @"yourRetinaImage"]; 

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

我已經更新了我的答案 – nvl

+0

謝謝看起來不錯,如果我緩存圖像,我需要下載後和緩存之前,使用上面的代碼,並且緩存2個圖像,經常和@ 2倍? – user3300062