首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >屏幕截图定义(x,y,宽度,高度)-MKAnnotationView

屏幕截图定义(x,y,宽度,高度)-MKAnnotationView
EN

Stack Overflow用户
提问于 2012-09-27 07:07:45
回答 1查看 222关注 0票数 0

正如你在下面看到的,代码会截取整个视图,但是我如何修改下面的代码来截取截图呢?例如,我想得到(x,y,width,height)--> (20,20,120,50)截图。我真正的问题是捕获注释部分的屏幕截图,并在第二个视图控制器中显示该区域。我只需要知道如何捕捉视图截图的一部分。提前谢谢。

代码语言: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;
}
EN

回答 1

Stack Overflow用户

发布于 2013-07-09 13:28:54

你可以通过提供CGRect参数来选择截图大小。

代码语言:javascript
复制
CGRect grabRect = CGRectMake(0,0,300,372); 
CGSize imageSize = grabRect.size;

干杯。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12611797

复制
相关文章

相似问题

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