2012-08-03 74 views
2

我從蘋果公司的網站上得到這個截圖方法:抓圖方法:跳繩狀態欄

- (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; 
} 

它的工作原理就像一個魅力,但我有一個小問題,它:我不想在狀態欄在我的屏幕截圖中顯示應用程序。我應該修改什麼?我真的不知道我應該在這裏修改什麼,因爲我從來沒有太多的CG框架。我在考慮減少20(狀態欄高度)的東西獲得「y」屬性,但我不想打破這種方法。那麼,我應該修改什麼來截取除狀態欄以外的所有內容?再次,我很抱歉如果這是一個真正的愚蠢的問題,但我真的不知道我會做什麼,我會嘗試自己修改它。

回答

4

我只是想通了。事實上,我只是中減去20從 「Y」 屬性:

所以不是這樣的:

CGContextTranslateCTM(context, [window center].x, [window center].y); 

我這樣做:

CGContextTranslateCTM(context, [window center].x, [window center].y - 20); 

有可能是一個更好的方式來做到這一點,但這一個非常好。

+0

將此標記爲您的答案,以幫助其他人將此問題確定爲已解決。謝謝! +1 – Stunner 2012-08-03 00:42:28

+1

我會的。但是我得先等兩天。 – 2012-08-03 00:42:52