以编程方式在iOS 5上进行截屏

4

我正在开发一个iOS 5 iPhone应用程序,希望在用户按下story board视图中的按钮时以编程方式进行截屏。我尝试了许多代码,但它们都是针对旧版本的iOS。如何在iOS 5中实现它。谢谢!


可能是重复的问题:如何以编程方式进行屏幕截图 - 一二三
1
@-一二三,帖子明确表示用户需要适用于iOS5的版本。 - Satyam
3个回答

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

1
UIGraphicsEndImageContext()在哪里? - Gargo

1

好的,有几种编程方式可以程序化地捕获iPhone屏幕

  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. 使用视图

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接