简体   繁体   English

从Core Data以pdf格式绘制带有CGRect的UIImage

[英]Drawing UIImage with CGRect in a pdf from Core Data

I'm generating a PDF document on the fly using data that's been outputted from my Core Data graph, however this is proving to be quite difficult. 我正在使用从“核心数据”图输出的数据即时生成PDF文档,但是事实证明这非常困难。

I'm using the assetlibrary to get the filepath of the image out of the core data graph by using the following: 我正在使用资产库通过以下操作从核心数据图中获取图像的文件路径:

NSURL *url = [[NSURL alloc] initWithString:bird.photo];
        ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

        [library assetForURL:url resultBlock:^(ALAsset *asset) {

                     _pdfBirdmage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];


                 } failureBlock:^(NSError *error) {

                     NSLog(@"Couldn't load asset %@ => %@", error, [error localizedDescription]);

                 }];

And this is showing that the image is as the following: 这表明该图像如下所示:

birdphotoview = <UIImage: 0x1caac0>

Obviously, I would like this to be in an image format, such as PNG or JPG in order to write it to the PDF document, using the following code: 显然,我希望它采用图像格式,例如PNG或JPG,以便使用以下代码将其写入PDF文档:

[_pdfBirdImage drawInRect:CGRectMake( (pageSize.width - _pdfBirdImage.size.width/2)/2, 350, _pdfBirdImage.size.width/2, _pdfBirdImage.size.height/2)];

However, this is not printing anything to the pdf. 但是,这没有将任何内容打印到pdf。

Any help would be brilliant. 任何帮助都是很棒的。

Are you using the CGPDF based routines? 您是否正在使用基于CGPDF的例程? I think the Y dimension is opposite in that event. 我认为在这种情况下Y维是相反的。 I've displayed PDFs before and overlaid data on top (and subsequently then rendered the entire screen back to disk but as an image). 我先显示了PDF,然后在其上覆盖了数据(随后将整个屏幕呈现为磁盘,但作为图像)。 If that is the case, I had to transform the existing PDF first. 如果是这种情况,我必须先转换现有的PDF。 Doesn't sound like your problem, but I don't see any PDF code referenced here, so hard to tell. 听起来不像是您的问题,但我看不到这里引用的任何PDF代码,所以很难说出来。 Regardless, was something along: 无论如何,总有一些事情:

UIGraphicsBeginImageContext(pdfPageRectScaled.size);
CGContextRef context = UIGraphicsGetCurrentContext();

// First fill the background with white.
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, pdfPageRectScaled);

CGContextSaveGState(context);
// Flip the context so that the PDF page is rendered
// right side up.
CGContextTranslateCTM(context, 0.0f, pdfPageRectScaled.size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);

// Scale the context so that the PDF page is rendered 
// at the correct size for the zoom level.
CGContextScaleCTM(context, pdfScale, pdfScale); 
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM