畫面區域的選擇 (Selection of screen area)


問題描述

畫面區域的選擇 (Selection of screen area)

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

‑‑‑‑‑

參考解法

方法 1:

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.

(by ClarenceDeepak Danduprolu)

參考文件

  1. Selection of screen area (CC BY‑SA 3.0/4.0)

#screenshot #XCode






相關問題

沒有 iPhone,如何使用 XCode 截屏? (Without iPhone, how to take screenshot with XCode?)

mapViewDidFinishLoadingMap:調用過早 (mapViewDidFinishLoadingMap: called too early)

屏幕截圖上的窗口沒有消失 (Window not disappearing on Screenshot)

Appium 中用於混合應用程序的多個屏幕截圖 (Multiple Screenshots in Appium for Hybrid Apps)

在Linux上通過Python腳本拍攝屏幕截圖 (Take a screenshot via a Python script on Linux)

如何使用服務器端腳本生成網頁的屏幕截圖? (How can I generate a screenshot of a webpage using a server-side script?)

如何使用 ruby 和 unix 服務器截取網頁截圖? (How do I take screenshots of web pages using ruby and a unix server?)

使用 jquery 生成網站的屏幕截圖 (generating a screenshot of a website using jquery)

畫面區域的選擇 (Selection of screen area)

使用 PHP 的網頁截圖? (Web Page Screenshots with PHP?)

iPhone:如何在使用 3d 圖層轉換時以編程方式拍攝屏幕截圖 (iPhone: how to take screen shots programmatically when using 3d layer transformations)

防止 VideoView Activity 中的視頻屏幕截圖 - Android (Prevent Video screen capture in VideoView Activity - Android)







留言討論