Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Tom Coomer
1,331 PointsCreate image from image and label
I would like to print an image that I would include the image and add the text from a label.
Here is the code I have to print the image.
-(void)printItem {
NSString *path = [[NSBundle mainBundle] pathForResource:@"St_Trop" ofType:@"jpg"];
NSData *dataFromPath = [NSData dataWithContentsOfFile:path];
UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];
if(printController && [UIPrintInteractionController canPrintData:dataFromPath]) {
printController.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
printController.printInfo = printInfo;
printController.showsPageRange = YES;
printController.printingItem = dataFromPath;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
}
};
[printController presentAnimated:YES completionHandler:completionHandler];
}
}
1 Answer
Mike Baxter
4,442 PointsWhat's the question? Are you just trying to get an image with text? If so, you'll probably want to use
UIGraphicsBeginImageContext and UIGraphicsGetImageFromCurrentImageContext
You'd also want to look into the NSString convenience methods for drawing text to a CG context.
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsI have managed to create the image now. I am trying to print this image but just can't work out how. Is there any way I can create a path for the created image?
Mike Baxter
4,442 PointsMike Baxter
4,442 PointsI'm not sure what you mean by a path for the image? When you use UIGraphicsGetImageFromCurrentImageContext, it returns a UIImage. It's not written to file, it's written to memory, so you don't need a path for it.