2012-07-16 52 views
2

默認情況下,UIImageView在UIImageView中將圖像顯示爲1px的image = 1pt,但我想顯示爲2px的image = 1pt。如何在UIImageView中顯示高分辨率圖像

以名稱「.. @ 2x ..」保存圖像的版本不適合,圖像不保存在文件系統中。

例如,圖像大小爲400x100,我想要在顯示中心顯示圖像,它應該是左側120磅和右側120點(640-400)/ 2

+0

也許你應該根據圖像大小調整UIImageView的框架。 – 2012-07-16 09:21:25

+0

我無法更改UIIMageView的框架。 UIImageView的內容模式是UIViewContentModeCenter。 例如,圖像大小爲400x100,我想在顯示中心顯示它,圖像的左側和右側有120 pt。 (640-400)/ 2 – George 2012-07-16 09:24:16

+0

您是否嘗試在文件名的末尾添加「@」符號?像保存到文件系統時的「myfile @ test.meta @」一樣? – Eugene 2012-07-16 09:25:30

回答

1

這是相當容易的,請注意下面的iOS 4版本,你沒有Retina顯示屏,這就是爲什麼在圖像縮放方法我我第一次做這個檢查:

//Retina detect 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){ 

然後有image從某處加載(文件,緩存等)我是縮放是這樣

UIImage * image2Xscaled = [UIImage alloc]; 
      image = [[image2Xscaled initWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp] autorelease]; 

的方法

initWithCGImage:scale:orientation: 

可在iOS版4.0。這就是爲什麼你需要第一次檢查。如果不支持銷售,請返回1.0縮放圖像。

0

您可以使用這樣的事情:

NSData *data = //your data, however you get that... 
UIImage *image = [UIImage imageWithData:data]; 
if ([UIScreen mainScreen].scale > 1.0f) 
{ 
    image = [UIImage imageWithCGImage:image 
           scale:[UIScreen mainScreen].scale 
          orientation:UIImageOrientationUp] 
} 
相關問題