2012-07-19 32 views
2

我有一個應用程序,可以在視網膜iPad(iPad3)上執行屏幕截圖。或者,也許我只是使用HOME + POWER進行屏幕截圖,然後以這種方式獲取圖像,這並不重要。最後,我拍攝的圖像是2048x1536 - 即1024x768 @ 2x。如何將視網膜捕獲的圖像重新繪製回視網膜並保持高質量?

現在我想將此圖像 - 這2048x1536圖像 - 顯示回屏幕上,我想保留它的可愛視網膜。

當我做這樣的事情(在Web瀏覽器中鍵入代碼)

UIImage *myImage = [UIImage imageWithContentsOfFile: pathToMyHiresImage]; 
UIImageView *myImageView = [[UIImageView alloc] initWithImage: myImage]; 
myImageView.frame = screenBounds; // make it fit 
// etc... 

我結束了一個低解析度(鋸齒狀對角線&文本)版本的圖像。

所以我的問題是:爲了在屏幕上顯示該圖像並讓它顯示@ 2x-DPI,因此它具有美麗的視網膜外觀,我需要做些什麼?

謝謝!

回答

1

這應該做的伎倆:

UIImage *myImage = [UIImage imageWithContentsOfFile: pathToMyHiresImage]; 
// *****Added Code***** 
myImage = [UIImage imageWithCGImage:myImage.CGImage scale:2 orientation:myImage.imageOrientation]; 
// ******************** 
UIImageView *myImageView = [[UIImageView alloc] initWithImage: myImage]; 
myImageView.frame = screenBounds; // make it fit 
// etc ... 

祝你好運!

編輯(OLIE) 下面是最後的代碼,以DavidH的建議考慮:

UIImage *myImage = [[UIImage alloc] initWithContentsOfFile: path]; 

if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) 
{ 
    float screenScale = [[UIScreen mainScreen] scale]; 
    if (screenScale > 1.) 
    { 
     id oldImage = myImage; 
     myImage = [[UIImage imageWithCGImage: myImage scale: screenScale orientation: myImage.imageOrientation] retain]; 
     [oldImage release]; 
    } 
} 
+1

Aaaand ...這就是*我需要的代碼行,非常感謝你!我希望我能接受@ 2x的答案! :) – Olie 2012-07-19 04:23:09

+1

如果([[UIScreen mainScreen] scale])== 2),則只應執行第二行。我真的很驚訝你不得不這樣做,UIImage並沒有開箱即用 - 這可能是iOS中的一個錯誤。 – 2012-07-19 12:08:52

+1

好的。我把最終的代碼放到凱爾的答案中。 – Olie 2012-07-19 18:33:26

1

這是石英(即真實數據)和UIImage膠合板相撞的地方。所以你得到你創建的UIImage。請求CGImage,然後查看該圖像。寬度和高度最好是2048x1536或者是錯的。

一旦你能弄清楚如何得到一個「真實」大小的圖像,你可以將它保存爲「[email protected]」,然後調整它一半,並保存爲「foo.png」。 UIImage讓事情變得非常容易,但是當你想要真正的控制時,你必須深入Quartz並在橡膠撞擊道路的地方工作。

我的猜測是你認爲你有一個視網膜圖像,但沒有,那麼當你去顯示它的一半雷茲。

+0

嗯,有一些代碼嗎?有問題的圖像,如果我通過Photos.app查看它,在屏幕上顯示正確的大小,並看起來像視網膜。它的大小是2048x1536。如果我使用我發佈的代碼,它看起來都是@ 1x-y。所以問題是:我如何在我的應用程序中獲得視網膜-Y外觀?謝謝! – Olie 2012-07-19 02:01:42