选择屏幕区域

时间:2011-07-04 11:59:44

标签: xcode screenshot

我正在尝试制作应用内截图,我有以下代码。

如何选择屏幕截图的区域?例如我想摆脱U -avigation栏和底部标签栏。我应该添加什么代码?

 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"];

1 个答案:

答案 0 :(得分:2)

您可以识别区域的rect并裁剪图像的那一部分,以获得所需的图像。

    ....

/* 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