2012-01-25 33 views
2

在我的應用程序中,我顯示相機,並使用UIGetScreenImage拍攝某些圖片的截圖,(我試過UIGraphicsGetImageFromCurrentImageContext,它對我的​​應用程序的幾乎任何部分的截圖效果都很好,但對於相機查看它只會返回一個空白圖像)......反正,我擔心蘋果會拒絕,因爲UIGetScreenImage的我的應用程序......我如何可以採取一個50像素的「截圖」 50像素箱從相機的左上角不使用這種方法?我搜索和所有我能找到的是「AVCaptureSession」我找不到太多關於做什麼,或者如果它甚至就是我正在尋找...任何見解? :) 多謝你們!!!iOS Capture「截圖」相機控制器

回答

3

它不會比蘋果的how to capture the camera's view文檔更清晰。是的,這涉及類AVCaptureSession

如果你確實需要的界面截圖,你應該在docs for that。從鏈接剪切和粘貼代碼(如果不工作,你應該提交給蘋果一個錯誤報告):

更新:它看來,這種做法已不再支持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

直接從蘋果的文檔。 –

+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 –