2013-08-26 89 views
2

我想獲取當前屏幕的截圖。如何在視圖控制器顯示在另一個視圖控制器上時獲取屏幕截圖?

但是,無論何時呈現MFMailComposeViewController或任何其他視圖控制器,繪製的屏幕截圖都沒有黑屏。

//Code to draw what ever is present currently on the screen 
- (UIImage*)screenshot { 

CGSize imageSize = [[UIScreen mainScreen] bounds].size; 


if (NULL != UIGraphicsBeginImageContextWithOptions) 
    UIGraphicsBeginImageContextWithOptions(imageSize,YES, 0.0); 
else 
    UIGraphicsBeginImageContext(imageSize); 



CGContextRef context = UIGraphicsGetCurrentContext(); 

// Iterate over every window from back to front 
for (UIWindow *currentWindow in [[UIApplication sharedApplication] windows]) 
{ 
    if (![currentWindow respondsToSelector:@selector(screen)] || [currentWindow screen] == [UIScreen mainScreen]) 
    { 



     CGContextSaveGState(context); 

     NSLog(@"current alpha %f", currentWindow.alpha); 

     CGContextSetAlpha(context, currentWindow.alpha); 

     // Center the context around the window's anchor point 
     CGContextTranslateCTM(context, [currentWindow center].x, [currentWindow center].y); 
     // Apply the window's transform about the anchor point 
     CGContextConcatCTM(context, [currentWindow transform]); 
     // Offset by the portion of the bounds left of and above the anchor point 
     CGContextTranslateCTM(context, 
           -[currentWindow bounds].size.width * [[currentWindow layer] anchorPoint].x, 
           -([currentWindow bounds].size.height) * [[currentWindow layer] anchorPoint].y); 

     // Render the layer hierarchy to the current context 
     [[currentWindow layer] renderInContext:context]; 

     // Restore the context 
     CGContextRestoreGState(context); 


    } 
} 

// Retrieve the screenshot image 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext().retain; 

UIGraphicsEndImageContext(); 

return [image autorelease];} 

當被呈現在視圖控制器,所述用於上述代碼迭代循環一次以上並得到了一個空白圖像被呈現mfmailcomposeviewcontroller時。儘管下面的視圖控制器的標籤欄控制器是可見的。

但是,當我按下郵件壓縮器的取消按鈕,屏幕上出現的操作表也沒有繪製。

這是由於UIGraphicsBeginImageContextWithOptions中的不透明度?

我想知道如何在顯示另一個視圖控制器時獲取屏幕圖像。

是不是默認的控制器屏幕截圖無法獲取?

請幫忙。

在此先感謝。

回答

0

那麼你可以簡單地添加

ViewOnTop.alpha = 0.0; 

到哪個是哪個,你要採取截圖的一個以上的觀點。這將首先使視圖頂部減少其阿爾法屬性從而使得它幾乎透明的,那麼你在這裏採取的截圖

添加上面的代碼

- (UIImage*)screenshot { 

ViewOnTop.alpha = 0.0; 
............ 
................... 
} 

編輯:

對於viewcontroller在相同的代碼中添加dismissViewController屬性到正在呈現的視圖,以便最終可以看到您要截圖的視圖。

+0

我想獲取MFMailComposeViewcontroller的屏幕快照,但每次拍攝屏幕截圖時,都會繪製一個完全黑屏。什麼都看不見。 – user1899840

相關問題