2012-02-15 116 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