开发者

Selection of screen area

开发者 https://www.devze.com 2023-03-17 16:30 出处:网络
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 co

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:UIGraphicsGetCu开发者_StackOverflowrrentContext()];
        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"];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号