Tuesday, September 24, 2013

GraphicsContext on a string to print to the problem

 
+ (Void) generatePDFByImg: (UIImage *) img andText: (NSString *) text
{
/ / Self.toolBar.hidden = YES;
/ / NSString * text = @ "puma are large cat-like animals which are found in Americ";

CGSize pdfSize;
pdfSize.height = 460;
pdfSize.width = 320;

UIGraphicsBeginImageContext ([[UIScreen mainScreen] bounds]. Size);
/ / [Self.view.layer renderInContext: UIGraphicsGetCurrentContext ()];
/ / UIImage * pdfImage = UIGraphicsGetImageFromCurrentImageContext ();
/ / UIGraphicsEndImageContext ();
/ / Self.iiimagev.image = pdfImage;

/ / UIImage * pdfImage = [UIImage imageNamed: @ "10074136.jpg"];

NSMutableData * outputData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData ((__bridge CFMutableDataRef) outputData);
CFMutableDictionaryRef attrDictionary = NULL;
attrDictionary = CFDictionaryCreateMutable (NULL, 0, & kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks);

CGContextRef pdfContext = CGPDFContextCreate (dataConsumer, NULL, attrDictionary);
CFRelease (dataConsumer);
CFRelease (attrDictionary);
UIImage * myUIImage = img;
CGImageRef pageImage = [myUIImage CGImage];
CGPDFContextBeginPage (pdfContext, NULL);
NSLog (@ "[myUIImage size]. Width =% f", [myUIImage size]. Width);
NSLog (@ "[myUIImage size]. Height =% f", [myUIImage size]. Height);
CGContextDrawImage (pdfContext, CGRectMake (0, [[UIScreen mainScreen] bounds]. Size.height, ([myUIImage size]. Width) * 2, ([myUIImage size]. Height) * 2), pageImage);
/ / NSLog (@ "[myUIImage size]. Width =% f", [myUIImage size]. Width);
/ / NSLog (@ "[myUIImage size]. Height =% f", [myUIImage size]. Height);
/ / CGContextDrawImage (pdfContext, CGRectMake (0, 0, [myUIImage size]. Width, [myUIImage size]. Height), pageImage);
/ / CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
/ / [Text drawInRect: CGRectMake ([myUIImage size]. Width, [myUIImage size]. Width, 30, 80) withFont: [UIFont boldSystemFontOfSize: 15]];

NSString * str = [NSString stringWithFormat: @ "pulma is lager cat-like animals:% @ seconds", @ "65555"];

/ / Select the font is 16pt Helvetica
CGContextSelectFont (pdfContext, "Helvetica", 16, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (pdfContext, kCGTextFill); / / set the drawing mode
/ / Set the text color to black
CGContextSetRGBFillColor (pdfContext, 0.0, 0.0, 200.0, 1.0);
/ / Convert str is a C string and display it

CGContextShowTextAtPoint (pdfContext, 10, 10, [str cStringUsingEncoding: [NSString defaultCStringEncoding]], str.length);

/ / CGContextRestoreGState (pdfContext); / / Restore the context

CGPDFContextEndPage (pdfContext);
CGPDFContextClose (pdfContext);
CGContextRelease (pdfContext);
/ / NSString * pdfFileName = [self getPDFFileName];
NSString * pdfFileName = [NSHomeDirectory () stringByAppendingPathComponent: @ "mytestpdf.pdf"];
[OutputData writeToFile: pdfFileName atomically: YES];

}


This code will generate an image and some text to PDF, use CGContextShowTextAtPoint (pdfContext, 10, 10, [str cStringUsingEncoding: [NSString defaultCStringEncoding]], str.length);
This method can be generated in English, but were unable to generate the Chinese, and I wanted to use [str drawAtPoint: point withFont: [UIFont systemFontOfSize: 16]] to achieve, but after the call does not appear in the PDF text
Which god know?

No comments:

Post a Comment