2012-02-15 89 views
4

目前我正在爲iPhones和iPads的Paint應用程序工作。我想要打印屏幕的當前屏幕顯示(繪圖)。在這種情況下,任何機構可以幫助我嗎?iOs打印當前屏幕

這是到目前爲止,我已經使用的代碼,

-(void)printImage { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"micky" ofType:@"png"]; 
    NSData *dataFromPath = [NSData dataWithContentsOfFile:path]; 

    UIPrintInteractionController *pCon = [UIPrintInteractionController sharedPrintController]; 

    if(pCon && [UIPrintInteractionController canPrintData:dataFromPath]) { 
     pCon.delegate = self; 
     UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
     printInfo.outputType = UIPrintInfoOutputGeneral; 
     printInfo.jobName = [path lastPathComponent]; 
     printInfo.duplex = UIPrintInfoDuplexLongEdge; 
     pCon.printInfo = printInfo; 
     pCon.showsPageRange = YES; 
     pCon.printingItem = dataFromPath; 

     void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { 
      if (!completed && error) { 
       NSLog(@"Unsuccessfull %@ error%u", error.domain, error.code); 
      } 
     }; 

     [pCon presentAnimated:YES completionHandler:completionHandler]; 

    } 
} 

同樣,所有我想知道的是,我應該能夠打印當前窗口(繪畫,屏幕),而不是形象米奇。 PNG(如代碼)

回答

3

此代碼對當前視圖的屏幕截圖,並輸出它的一個UIImage

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
[self.view.layer renderInContext:context]; 
UIImage *imageFromCurrentView = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

感謝名單這就是工作像魅力。我想多加強一點。如果我想在當前屏幕中獲取特定視圖的屏幕截圖,那我該怎麼做? – BigAppleBump 2012-02-17 03:01:30

+0

用你想抓取的任何視圖替換「self.view」的出現次數。請將此答案標記爲已接受:) – samvermette 2012-02-17 04:51:49

+0

是的。非常感謝你 – BigAppleBump 2012-02-17 05:33:17