2011-11-27 74 views
4

我正在開發一個iOs 5 iPhone應用程序,我想以編程方式在用戶按下故事板中的視圖按鈕時截圖。我已經嘗試了很多代碼,但它們適用於舊版本的iOs。我怎樣才能做到這一點在iOs 5.謝謝!以編程方式採用屏幕截圖iOs 5

+0

可能重複[如何採取截圖編程(http://stackoverflow.com/questions/2200736/how-to-take-a-screenshot - 程序化) –

+1

@ - 一二三,該帖子明確表示,用戶需要它的iOS5 – Satyam

回答

12
- (void)drawRect:(CGRect)rect { 
    UIGraphicsBeginImageContext(self.bounds.size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 
+1

其中是UIGraphicsEndImageContext(); ? – Gargo

+0

修正了它,我的不好。 –

1

嗯,有編程

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

  2. 捕捉iPhone屏幕的一些方法使用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. 使用視圖

    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(); 
    
  5. 使用窗

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

在對這些5點的方法,我發現了第一選項的複製粘貼和用於壓縮的施加電平非常方便第一種方法爲圖像提供真實的像素數據。

0

由於這是一個老問題,我把代碼放在這裏,希望能幫助別人。 (代碼是從蘋果副本:http://developer.apple.com/library/ios/#qa/qa1703/_index.html)的

- (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; 
} 
相關問題