一个关于字符串打印到GraphicsContext上的问题

iOS 码拜 8年前 (2015-11-16) 868次浏览
+ (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"];
    
    //选择字体为16pt Helvetica
    CGContextSelectFont(pdfContext, "Helvetica", 16, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(pdfContext, kCGTextFill);     //设置绘制模式
    //设置文本颜色为黑色
    CGContextSetRGBFillColor(pdfContext, 0.0, 0.0, 200.0, 1.0);
    //转换str为一个C 字符串并显示它
    
    CGContextShowTextAtPoint(pdfContext,  10 , 10, [str cStringUsingEncoding:[NSString defaultCStringEncoding]], str.length);
    
    //    CGContextRestoreGState(pdfContext);    //恢复上下文
    
    CGPDFContextEndPage(pdfContext);
    CGPDFContextClose(pdfContext);
    CGContextRelease(pdfContext);
   // NSString* pdfFileName = [self getPDFFileName];
    NSString *pdfFileName = [NSHomeDirectory() stringByAppendingPathComponent: @"mytestpdf.pdf"];
    [outputData writeToFile:pdfFileName atomically:YES];
}

这段代码实现将一个图片和一些文字生成到PDF中,使用CGContextShowTextAtPoint(pdfContext,  10 , 10, [str cStringUsingEncoding:[NSString defaultCStringEncoding]], str.length);
这个方法可以生成英文,但是无法生成中文,本人就想用 [str drawAtPoint:point withFont:[UIFont systemFontOfSize:16]]来实现,但是调用之后PDF中并没有出现文字
哪位高手懂?

解决方案:30分
,厚脸接个分
解决方案:30分
求分享…..

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明一个关于字符串打印到GraphicsContext上的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)