I've one question. Is it possible to print an image which is only temporarily saved? I saved the image as it is done in this example of a question on stackoverflow.
But when I try to use this picture for printing via AirPrint it doesn't work.
- (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];
}
}
But the printer is able to print (I tested it with a fix image)...so the printer not seems to be the problem. Thanks for your help in advance.
UPDATE I did some changes to the code/added a few code lines:
[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;
But it only works when i set writeToFile: @"test.png"
...but then I can only save an image once and not overwrite it.
When I do it with the code above there is one error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'