CGPDFContextCreateWithURL 未正确创建上下文

发布于 2024-08-26 10:31:58 字数 697 浏览 1 评论 0原文

使用以下代码,未正确创建 ctx。它仍然是nil

#import <QuartzCore/QuartzCore.h>

@implementation UIView(PDFWritingAdditions)

- (void)renderInPDFFile:(NSString*)path
{
    CGRect mediaBox = self.bounds;
    CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL URLWithString:path], &mediaBox, NULL);

    CGPDFContextBeginPage(ctx, NULL);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
    [self.layer renderInContext:ctx];
    CGPDFContextEndPage(ctx);
    CFRelease(ctx);
}

@end

在控制台中它显示: :CGPDFContextCreate:无法创建 PDF 上下文委托。

我尝试了几种不同的路径,所以我相当确定这不是问题。感谢您的任何建议!

Using the following code, the ctx is not correctly being created. It remains nil:

#import <QuartzCore/QuartzCore.h>

@implementation UIView(PDFWritingAdditions)

- (void)renderInPDFFile:(NSString*)path
{
    CGRect mediaBox = self.bounds;
    CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL URLWithString:path], &mediaBox, NULL);

    CGPDFContextBeginPage(ctx, NULL);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
    [self.layer renderInContext:ctx];
    CGPDFContextEndPage(ctx);
    CFRelease(ctx);
}

@end

In the console it shows:
<Error>: CGPDFContextCreate: failed to create PDF context delegate.

I've tried several different paths so I'm fairly certain that is not the problem. Thanks for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2024-09-02 10:31:58

如果字符串包含路径,则需要 +[NSURL fileURLWithPath:],而不是 +[NSURL URLWithString:]。

If your string contains a path, you want +[NSURL fileURLWithPath:], not +[NSURL URLWithString:].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文