为什么 UIImage 上的 drawPatternInRect: 仅适用于 Views 上下文
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cocoa 中还有一个与图形上下文相关的附加概念。在 Core Graphics 中,必须将 CGContextRef 作为显式参数传递给每个 CG 绘图调用。在 iOS 的许多其他地方,都有当前上下文的概念,它是全局当前上下文(每个线程全局),作为许多绘图调用的隐含参数。包括 UIImage 在内的许多 API 都在当前上下文中绘制。请参阅标题 UIGraphics.h。
通常,当前上下文是在调用drawRect:之前由视图系统为您设置的,这就是为什么绘图在这些情况下似乎可以工作的原因。如果您自己创建了上下文,则需要显式设置当前上下文。此代码应该适合您:
在 Mac 上,使用 NSGraphics 上下文的情况类似,请参阅 Apple 的文档 NSGraphicsContext。
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:
On the Mac things are similar using NSGraphics context, See Apple's documentation for NSGraphicsContext.