首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调整应用中屏幕截图的大小

调整应用中屏幕截图的大小
EN

Stack Overflow用户
提问于 2012-03-16 14:09:54
回答 2查看 1.9K关注 0票数 1

我在拍摄特定区域的截图时遇到了问题。我在底部有一个标签栏,在顶部有一个工具栏。我想要在这两个条之间的屏幕截图。下面是我的代码。我如何改进我的代码来裁剪出条形图呢?

谢谢

代码语言:javascript
复制
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();




UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-16 16:24:54

我想你可以试着在创建最终的UIImage之后裁剪出你需要的部分。

代码语言:javascript
复制
CGRect contentRectToCrop = CGRectMake(0, 44, 320, 392);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], contentRectToCrop);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
票数 3
EN

Stack Overflow用户

发布于 2012-03-16 15:51:36

未经测试的建议:

代码语言:javascript
复制
imageSize.origin.y+=44; //toolbar size
imageSize.size.height-=88; //tabbar+toolbar size
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9732735

复制
相关文章

相似问题

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