iOS Capture摄像头控制器的“截图”
在我的应用程序中,我显示相机,我使用UIGetScreenImage(我试过UIGraphicsGetImageFromCurrentImageContext,它几乎在我的应用程序的几乎任何部分的截图工作,但相机视图它只会返回一个空白的白色图像)的截图。 ..无论如何,我担心苹果将拒绝我的应用程序,因为UIGetScreenImage …我怎样才能不使用此方法从相机的左上angular50像素的50px框“截图”? 我search了所有我能find的是“AVCaptureSession”,我找不到什么,或者甚至是我正在寻找什么…任何见解? :) 多谢你们!!!
它没有比苹果公司的文档更清楚如何捕捉相机的视图 。 是的,这涉及类AVCaptureSession
。
如果您确实需要界面的屏幕截图,则应该参阅相关文档 。 从链接中剪下并粘贴代码(如果这不起作用,您应该向苹果提交一个错误报告):
更新 :似乎这种方法不再支持新版本的iOS。 第二个环节现在也被打破了。
- (UIImage*)screenshot { // Create a graphics context with the target size // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext CGSize imageSize = [[UIScreen mainScreen] bounds].size; if (NULL != UIGraphicsBeginImageContextWithOptions) UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); else UIGraphicsBeginImageContext(imageSize); CGContextRef context = UIGraphicsGetCurrentContext(); // Iterate over every window from back to front for (UIWindow *window in [[UIApplication sharedApplication] windows]) { if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) { // -renderInContext: renders in the coordinate space of the layer, // so we must first apply the layer's geometry to the graphics context CGContextSaveGState(context); // Center the context around the window's anchor point CGContextTranslateCTM(context, [window center].x, [window center].y); // Apply the window's transform about the anchor point CGContextConcatCTM(context, [window transform]); // Offset by the portion of the bounds left of and above the anchor point CGContextTranslateCTM(context, -[window bounds].size.width * [[window layer] anchorPoint].x, -[window bounds].size.height * [[window layer] anchorPoint].y); // Render the layer hierarchy to the current context [[window layer] renderInContext:context]; // Restore the context CGContextRestoreGState(context); } } // Retrieve the screenshot image UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
自iOS7以来,您可以使用:
drawViewHierarchyInRect
UIImage *image; UIGraphicsBeginImageContext(self.view.frame.size); [self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:YES]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();