2012-07-19 68 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