[英]How to get the screenshot when a view controller is presented over another view controller?
我想獲取當前屏幕的屏幕快照。
但是,無論何時出現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];}
呈現視圖控制器時,以上代碼中的for循環重復進行多次,並且呈現mfmailcomposeviewcontroller時得到空白圖像。 盡管下面的視頻控制器的標簽欄控制器是可見的。
但是,當我按下郵件編輯器的“取消”按鈕時,也不會繪制屏幕上顯示的操作表。
這是由於UIGraphicsBeginImageContextWithOptions中的不透明性嗎?
我想知道當另一個視圖控制器出現時如何獲取屏幕圖像。
是否無法獲得默認控制器的屏幕截圖?
請幫忙。
提前致謝。
好吧,您只需添加
ViewOnTop.alpha = 0.0;
到您要截屏的視圖上方的視圖。 這將首先使“ 頂部視圖”將其alpha屬性減小為0,從而使其幾乎透明,然后截取屏幕截圖
在上面添加上面的代碼
- (UIImage*)screenshot {
ViewOnTop.alpha = 0.0;
............
...................
}
編輯:
對於同一代碼中的viewcontroller
,將dismissViewController
屬性添加到要顯示的視圖中,以便最終可以看到要截屏的視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.