屏幕区域的选择

发布于 2024-11-18 03:43:02 字数 738 浏览 1 评论 0原文

我正在尝试制作应用内屏幕截图,其中包含以下代码。

如何选择截图的区域?例如,我想摆脱 UInavigation 栏和底部选项卡栏。我应该添加什么代码?

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

        NSData * imageData = UIImageJPEGRepresentation(image, 1.0);

        if ( [MFMailComposeViewController canSendMail] ) {
            MFMailComposeViewController * mailComposer = [[MFMailComposeViewController alloc] init];
            mailComposer.mailComposeDelegate = self;
            [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"];

I am trying to make a in-app screenshot which I have the following codes.

How can to select the area for the screenshot? e.g. I want to get rid of the UInavigation bar and the bottom tabbar. What code should I add?

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

        NSData * imageData = UIImageJPEGRepresentation(image, 1.0);

        if ( [MFMailComposeViewController canSendMail] ) {
            MFMailComposeViewController * mailComposer = [[MFMailComposeViewController alloc] init];
            mailComposer.mailComposeDelegate = self;
            [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2024-11-25 03:43:02

您可以识别该区域的矩形并裁剪图像的该部分以获得您需要的图像。

    ....

/* Identify the region that needs to be cropped */
CGRect navigationBarFrame = self.navigationController.navigationBar.frame;
CGRect tabBarFrame = self.tabBarController.tabBar.frame;

CGRect screenshotFrame;
screenshotFrame.origin.x = 0;
screenshotFrame.origin.y = navigationBarFrame.size.height;
screenshotFrame.size.width = navigationBarFrame.size.width;
screenshotFrame.size.height = tabBarFrame.origin.y - screenshotFrame.origin.y;

/* Crop the region */
CGImageRef screenshotRef = CGImageCreateWithImageInRect(image, screenshotFrame);
UIImage * screenshot = [[(UIImage *)screenshotRef retain] autorelease];
CGImageRelease(screenshotRef);

/* Convert to data and send */
NSData * screenshotData = UIImageJPEGRepresentation(screenshot, 1.0);

if ( [MFMailComposeViewController canSendMail] ) {
       ....
    [mailComposer addAttachmentData:screenshotData 
                           mimeType:@"image/jpeg"
                           fileName:@"attachment.jpg"];
       ....
}

如果您手动使用导航栏和/或选项卡栏,请相应地替换 self.navigationController.navigationBarself.tabBarController.tabBar

You can identify the rect of the region and crop that part of the image to get the image you need.

    ....

/* Identify the region that needs to be cropped */
CGRect navigationBarFrame = self.navigationController.navigationBar.frame;
CGRect tabBarFrame = self.tabBarController.tabBar.frame;

CGRect screenshotFrame;
screenshotFrame.origin.x = 0;
screenshotFrame.origin.y = navigationBarFrame.size.height;
screenshotFrame.size.width = navigationBarFrame.size.width;
screenshotFrame.size.height = tabBarFrame.origin.y - screenshotFrame.origin.y;

/* Crop the region */
CGImageRef screenshotRef = CGImageCreateWithImageInRect(image, screenshotFrame);
UIImage * screenshot = [[(UIImage *)screenshotRef retain] autorelease];
CGImageRelease(screenshotRef);

/* Convert to data and send */
NSData * screenshotData = UIImageJPEGRepresentation(screenshot, 1.0);

if ( [MFMailComposeViewController canSendMail] ) {
       ....
    [mailComposer addAttachmentData:screenshotData 
                           mimeType:@"image/jpeg"
                           fileName:@"attachment.jpg"];
       ....
}

If you are manually using a navigation bar and/or tab bar then replace the self.navigationController.navigationBar and self.tabBarController.tabBar appropriately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文