拍摄屏幕截图(iOS应用程序)

拍摄屏幕截图(iOS应用程序),ios,objective-c,uiimageview,uiimage,screenshot,Ios,Objective C,Uiimageview,Uiimage,Screenshot,我正在开发一个应用程序,我想拍摄屏幕的即时截图。我读了一些关于这方面的教程,发现了这种解决方案: UIImageView * Test = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; UIGraphicsBeginImageContext(CGSizeMake(320,480)); CGContextRef context = UIGraphicsGetCurrentContext(); [[self.view

我正在开发一个应用程序,我想拍摄屏幕的即时截图。我读了一些关于这方面的教程,发现了这种解决方案:

UIImageView * Test = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UIGraphicsBeginImageContext(CGSizeMake(320,480));
CGContextRef context = UIGraphicsGetCurrentContext();
[[self.view layer] renderInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Test.image = screenShot;
[self.view addSubview:Test];
但是这段代码并没有给我和在你的设备上按下home键和其他按钮来制作屏幕截图一样的东西。我怎样才能修好它


编辑:我已经加入了QuartzCore/QuartzCore.h

看到了吗?它并没有像你按home和其他按钮那样做一个精确的截图。这两个截图之间有什么不同?屏幕上有哪些组件?仅供参考-不要硬编码屏幕大小和比例。你的代码只对非视网膜的3.5英寸iPhone/iPod touch有效。区别在于我有一个3D立方体,用CALayers完成,我只有屏幕截图上的初始视图,即使我放了:[[self.view.layer presentationLayer]renderContext:context];它不接受计数旋转等