首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式截图iOs 5

以编程方式截图iOs 5
EN

Stack Overflow用户
提问于 2011-11-27 21:27:16
回答 3查看 5.6K关注 0票数 4

我正在开发一个iOS5 iPhone应用程序,我想在用户按下故事板中视图的按钮时以编程方式截图。我尝试过很多代码,但它们都是针对iOs的老版本。我在iOS5里怎么做?谢谢!!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-28 02:11:27

代码语言:javascript
复制
- (void)drawRect:(CGRect)rect {   
     UIGraphicsBeginImageContext(self.bounds.size);
     [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();  
     UIGraphicsEndImageContext();   
}
票数 12
EN

Stack Overflow用户

发布于 2013-07-12 18:25:36

嗯,有几种方法可以通过编程来捕获iPhone屏幕

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

  • Using
  1. AVFoundation OpenGL OpenGL ES视图

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();newImage窗口

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

在这5种方法中,我发现第一种方法对于复制-粘贴和应用压缩级别非常方便,因为第一种方法给出的图像是真实的像素数据。

票数 1
EN

Stack Overflow用户

发布于 2013-10-11 14:44:56

因为这是一个老问题,所以我把代码放在这里,希望能对大家有所帮助。(代码从苹果公司复制:http://developer.apple.com/library/ios/#qa/qa1703/_index.html)

代码语言:javascript
复制
- (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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8285889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档