为什么 UIImage 上的 drawPatternInRect: 仅适用于 Views 上下文

发布于 2024-10-11 08:01:33 字数 396 浏览 8 评论 0原文

我正在使用 CGBitmapContextCreate 调用创建自己的上下文。上下文已成功创建。我可以使用调用 CGContextDrawImage 在此上下文中成功绘制 UIImage。但是,当我尝试使用 UIImage 的调用 drawPatternInRect: 时,它给出错误“Context is nil”。

我确信上下文不为零并且它已正确创建,因为 CGContextDrawImage 在相同的上下文中工作正常。仅当上下文为 UIView 或我在 UIView 的 drawRect 方法中创建上下文时,drawPatternInRect: 调用才能正常工作。但是,我无法使用 UIView 的上下文,因为我需要从该上下文中生成 UIImage。

有谁知道这里的问题是什么或drawPatternInRect的任何其他替代简单方法?

I am creating my own context using CGBitmapContextCreate call. The context is created successfully. I can draw an UIImage in this context using the call CGContextDrawImage successfully. But, when I try to use the call drawPatternInRect: of UIImage, it gives the error 'Context is nil'.

I am sure that context is not nil and it is created properly because, CGContextDrawImage is working fine in the same context. The drawPatternInRect: call works fine only when the context is of UIView OR if I create the context in drawRect method of UIView. But, I cannot use UIView's context since I need to generate UIImage out of this context.

Does anyone knows what is the problem here OR any other alternate simple method for drawPatternInRect?

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

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

发布评论

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

评论(1

冰魂雪魄 2024-10-18 08:01:33

Cocoa 中还有一个与图形上下文相关的附加概念。在 Core Graphics 中,必须将 CGContextRef 作为显式参数传递给每个 CG 绘图调用。在 iOS 的许多其他地方,都有当前上下文的概念,它是全局当前上下文(每个线程全局),作为许多绘图调用的隐含参数。包括 UIImage 在内的许多 API 都在当前上下文中绘制。请参阅标题 UIGraphics.h。

通常,当前上下文是在调用drawRect:之前由视图系统为您设置的,这就是为什么绘图在这些情况下似乎可以工作的原因。如果您自己创建了上下文,则需要显式设置当前上下文。此代码应该适合您:

UIGraphicsPushContext( yourCGContext );

// Do your drawing here

UIGraphicsPopContext();

在 Mac 上,使用 NSGraphics 上下文的情况类似,请参阅 Apple 的文档 NSGraphicsContext

NSGraphicsContext* nsGraphicsContext = [ NSGraphicsContext 
        graphicsContextWithGraphicsPort: yourCGContext flipped: NO ];
[ NSGraphicsContext saveGraphicsState ];
[ NSGraphicsContext setCurrentContext: nsGraphicsContext ];

// Do your drawing here

[ NSGraphicsContext restoreGraphicsState ];

There is an additional concept relating to graphics contexts in Cocoa. In Core Graphics there is the CGContextRef which has to be passed to every CG drawing call as an explicit parameter. In many other places in iOS there is the concept of the current context which is a global current context (global for each thread) as an implied parameter to many drawing calls. Many APIs including UIImage draw in the current context. See the header UIGraphics.h.

Normally the current context is setup for you by the view system before drawRect: is called which is why drawing seems to work in these cases. In the case where you have created the context yourself you need to explicitly setup the current context. This code should work for you:

UIGraphicsPushContext( yourCGContext );

// Do your drawing here

UIGraphicsPopContext();

On the Mac things are similar using NSGraphics context, See Apple's documentation for NSGraphicsContext.

NSGraphicsContext* nsGraphicsContext = [ NSGraphicsContext 
        graphicsContextWithGraphicsPort: yourCGContext flipped: NO ];
[ NSGraphicsContext saveGraphicsState ];
[ NSGraphicsContext setCurrentContext: nsGraphicsContext ];

// Do your drawing here

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