2012-01-25 5 views
2

В моем приложении я показываю камеру, и я делаю скриншоты некоторых пертов с помощью UIGetScreenImage (я попробовал UIGraphicsGetImageFromCurrentImageContext и отлично работает для скриншотов практически для любой части моего приложения, но для камеры просмотр просто вернет пустое белое изображение) ... В любом случае, я опасаюсь, что Apple отклонит мое приложение из-за UIGetScreenImage ... Как я могу снять «скриншот» окна размером 50 пикселей на 50 пикселей из верхнего левого угла камеры без использования этого метода? Я искал, и все, что я мог найти, это «AVCaptureSession», и я не мог много узнать о том, что это делает, или если это даже то, что я ищу ... Любое понимание? :) Спасибо, парни!!!iOS Capture «скриншот» контроллера камеры

ответ

3

Это не становится намного яснее, чем документы Apple на how to capture the camera's view. Да, это связано с классом AVCaptureSession.

Если вам действительно нужен скриншот интерфейса, вы должны взять его у docs for that. Вырезать и вставить код из ссылки (если это не работает, вы должны представить отчет об ошибке в Apple):

Update: Это не кажется, что такой подход больше не поддерживается на более новых версиях 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; 
} 
+0

Хехе! :) Спасибо!!! –

+0

Непосредственно из документов Apple. –

+1

@DummyCode: Я использую этот же метод, но не фиксирует предварительный просмотр камеры. – TamilKing

-1

С iOS7 вы можете использовать:

drawViewHierarchyInRect

UIImage *image; 
UIGraphicsBeginImageContext(self.view.frame.size); 
[self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:YES]; 
image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+4

Он рисует все (даже данные OpenGL) в UIImage, за исключением предварительного просмотра камеры. iOS 8.1 –

Смежные вопросы