我有一个问题。是否可以打印仅暂时保存的图像? 我在图像溢出的问题example中完成了保存图像。
但是,当我尝试使用此图片通过AirPrint进行打印时,它无效。
- (IBAction)btnPrintTapped:(id)sender {
UIImage* image = nil;
UIGraphicsBeginImageContext(_scrollView.contentSize);
{
CGPoint savedContentOffset = _scrollView.contentOffset;
CGRect savedFrame = _scrollView.frame;
_scrollView.contentOffset = CGPointZero;
_scrollView.frame = CGRectMake(0, 0, _scrollView.contentSize.width, _scrollView.contentSize.height);
[_scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
_scrollView.contentOffset = savedContentOffset;
_scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext();
if (image != nil) {
[UIImagePNGRepresentation(image) writeToFile: @"/tmp/test.png" atomically: YES];
UIImage *yourImage = [UIImage imageNamed:@"/tmp/test.png"];
NSArray *itemsToShare = @[yourImage];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = @[ UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo];
[self presentViewController:activityVC animated:YES completion:nil];
}
}
但打印机能够打印(我用修复图像对其进行了测试)......所以打印机似乎不是问题所在。 感谢您的帮助。
更新 我对代码做了一些更改/添加了一些代码行:
[UIImagePNGRepresentation(image) writeToFile: @"/tmp/test.png" atomically: YES];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:@"/tmp/test.png" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
UIImage *ImageItem = image;
但它仅在我设置writeToFile: @"test.png"
时有效...但是我只能保存一次图像而不会覆盖它。
当我使用上面的代码时,有一个 错误 :
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSPlaceholderArray initWithObjects:count:]:尝试从对象[0]中插入nil对象
答案 0 :(得分:0)
经过一番尝试后,它终于有效了......
if (image != nil) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
@"test.png" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
UIImage* image = [UIImage imageWithContentsOfFile:path];
UIImage *yourImage = image;