【发布时间】:2014-06-29 20:29:38
【问题描述】:
假设您有一个只有文本(即 UILabel)和一些黑线的简单 UIView。
这正是你如何打印 UIView...
将其渲染为 UIImage,然后打印...
- (IBAction)printB:(id)sender
{
// we want to print a normal view ... some UILabels, maybe a black line
// in this technique, depressingly we CREATE AN IMAGE of the view...
// step 1. make a UIImage, of the whole view.
UIGraphicsBeginImageContextWithOptions(self.printMe.bounds.size, NO, 0.0);
// [self.printMe.layer renderInContext:UIGraphicsGetCurrentContext()];
// UIImage *asAnImage = UIGraphicsGetImageFromCurrentImageContext();
// .... or, more futuristically.....
[self.printMe drawViewHierarchyInRect:self.printMe.bounds
afterScreenUpdates:NO];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// step 2. choose grayscale, etc
UIPrintInfo *info = [UIPrintInfo printInfo];
info.orientation = UIPrintInfoOrientationPortrait;
info.outputType = UIPrintInfoOutputGrayscale;
// step 3, print that UIImage
UIPrintInteractionController *pic =
[UIPrintInteractionController sharedPrintController];
pic.delegate = self;
//pic.printingItem = asAnImage;
pic.printingItem = snapshotImage;
pic.printInfo = info;
UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
{
if (error)
NSLog(@"failed... %@ %ld", error.domain, (long)error.code);
if (completed)
NSLog(@"completed yes");
else
NSLog(@"completed no");
};
[pic presentAnimated:YES completionHandler:completionHandler];
}
但这会给你带来糟糕的结果。这是打印排版的一种荒谬方式。它应该作为附言或其他东西发送。
在 iPad 屏幕上...
打印时..
请注意,当然,如果您使用视网膜 ipad,它会好一些,但这不是重点。把文字信息,黑线,作为图像打印出来是很可笑的。
现在,您可能认为可以打印类似这样的 UIView ...
pic.printFormatter = [someUIView viewPrintFormatter];
但无论我如何尝试,我都无法让它发挥作用。
有人对此有任何意见吗?谢谢!
PS - 请注意,在 iOS 中,我们最近已从使用 renderInContext 更改为使用快照调用。这对这里的问题完全没有影响,干杯。
【问题讨论】:
-
为什么你会认为有格式化程序,你到底想达到什么目的?无论如何创建一个PDF文件怎么样? stackoverflow.com/questions/5443166/…
-
我认为像 PDF 这样的东西是你目前最好的选择,但你需要使用路径之类的东西,因为它使用矢量并且在缩放时不会丢失分辨率。我很确定这是可能的,但很难将任何视图类型绘制为 PDF 元素。问题是 iOS 使用一些内部协议在上下文中绘制视图,这与 PDF 过程非常相似,我什至记得有人曾经找到一种将 PS 图层导入 iOS 的方法,因此所有线条、渐变等都完全像在 PS 中一样绘制。
-
iOS 中的这种“格式”在某种意义上是用 UIView 等类编写的,每个类都有一些额外的参数用于描述如何绘制它们(不会丢失分辨率)。可能是界面构建器具有您正在寻找的某种特定格式(尽管我对此表示怀疑,因为它根本不需要可移植性)但即使它确实存在,除了 Xcode 之外没有其他应用程序可以读取它。
-
对,这就是我要问的,希望对此有深入了解的人会出现。现在干杯。
-
我只知道确实存在的东西,我不能确定这样的东西不存在,因为我可能根本没有听说过 if :)
标签: ios printing uiprintinfo