2012-11-08 36 views
8

我同時使用UIKit控制和OpenGL一個UIView。我想以編程方式獲得該視圖的截圖。如何在iOS中以編程方式截圖?

如果我使用UIGraphicsGetImageFromCurrentImageContext(),在OpenGL內容是空白的; 如果我使用glReadPixels(...)方法,則UIKit內容爲空;

我很困惑,如何把一個完整的屏幕截圖。 謝謝!

回答

0

這裏是你如何利用一個簡單的截圖編程方式UIImage的。如果你想讓它用於任何其他物體,用它代替UIImage

.h文件添加NSData *Data;然後在.m文件添加到您的代碼

UIGraphicsBeginImageContext(self.view.frame.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *attachimage = [[UIImage alloc]initWithData:Data]; 
UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
attachimage = viImage; 
UIImageWriteToSavedPhotosAlbum(viImage, nil, nil, nil); 
+0

只需要UIKit中條件的影響,對OpenGL的頁面,只拿到空白圖像 – coodi8

2

你得到結果的UIImage沒有圖像丟失質量

- (UIImage *)captureView:(UIView *)view { 
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return img; 
    } 
+0

它並不需要在OpenGL頁面 – coodi8

+0

你使用GLGridview影響? – kb920

+0

不,只是常見的OpenGL視圖 – coodi8

1
UIGraphicsBeginImageContext(self.view.bounds.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
NSData *data = [UIImagePNGRepresentation(image) retain]; 
UImage *screenShot = [UIImage imageWithData:data]; 
+0

它只對UIKit條件產生影響,對於OpenGL頁面只產生空白圖像 – coodi8

0

你知道在哪裏OpenGL內容位於您的視圖中。所以應該很容易將glReadPixels的結果複製到UIKit的快照中。

+0

我正在開發一個截圖控件,我不知道該視圖是通過UIKit還是OpenGL實現的,還是混合。如果我能得到視圖通過什麼實現的信息,問題就會解決。 – coodi8

+0

@ coodi8我認爲這可能是一個問題。因爲OpenGL直接在圖形芯片上渲染,所以不會那麼容易。 UI圖形不會在圖形芯片上呈現。 (AFAIK) –

8

使用下面的代碼採取截屏

-(UIImage *) screenshot 
    { 

      CGRect rect; 
      rect=CGRectMake(0, 0, 320, 480); 
      UIGraphicsBeginImageContext(rect.size); 

      CGContextRef context=UIGraphicsGetCurrentContext(); 
      [self.view.layer renderInContext:context]; 

      UIImage *image=UIGraphicsGetImageFromCurrentImageContext(); 
      UIGraphicsEndImageContext(); 

      return image; 
    } 
+0

這段代碼只是爲了獲取UIKit的東西......這個問題更復雜,並且很好地解釋了:他還需要採用OpenGL的東西。 –

4

那麼,捕捉iPho的方法很少NE屏幕編程

  1. 使用UIKit的http://developer.apple.com/library/ios/#qa/qa1703/_index.html

  2. 使用AVFoundation框架http://developer.apple.com/library/ios/#qa/qa1702/_index.html

  3. 利用OpenGL ES http://developer.apple.com/library/ios/#qa/qa1704/_index.html

  4. 從iOS的7起,您還可以使用Why does my programmatically created screenshot look so bad on iOS 7?

  5. 使用的UIWindow

    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    UIGraphicsBeginImageContext(screenRect.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [[UIColor blackColor] set]; 
    CGContextFillRect(ctx, screenRect); 
    [self.window.layer renderInContext:ctx]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    
  6. 使用的UIView

    UIGraphicsBeginImageContext(self.view.frame.size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    

出的這6個選項,我找到了第一個選項的複製粘貼和施加壓力的級別爲第方法非常方便給圖像提供真實的像素數據。我也喜歡選項4,因爲API帶有SDK。

+1

感謝你的看來,使用子視圖控制器的屏幕不會自動添加子視圖控制器圖層。但使用窗口似乎適用於全屏圖像。謝謝! – slycrel

5
-(UIImage *) screenshot 
{ 
CGImageRef UIGetScreenImage(void); 
CGImageRef screen = UIGetScreenImage(); 
UIImage* screenImage = [UIImage imageWithCGImage:screen]; 
CGImageRelease(screen); // you need to call this. 
return screenImage; 
} 
+0

有人可以解釋它是如何工作的?還需要修復方向 – ZYiOS

+0

絕對不是這樣[這是爲什麼](http://stackoverflow.com/questions/3767860/uigetscreenimage-the-app-is-not-getting-the-approval-due-to - 該方法) –

相關問題