2011-07-04 117 views
1

我想做一個應用程序中的屏幕截圖,我有以下代碼。屏幕區域的選擇

如何選擇截圖區域?例如我想擺脫UInavigation欄和底部的tabbar。我應該添加什麼代碼?

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

回答

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適當。

+0

非常詳細和有幫助。非常感謝你。 – Clarence

+0

你好,我必須開發相同的應用程序。你能幫我解決問題嗎? –