简体   繁体   English

使用CGBitmapContextCreate()和基础位图内存管理创建的CGContextRef

[英]CGContextRef created with CGBitmapContextCreate() & underlying bitmap memory management

I'm trying to get used to iOS's CGContextRef graphic contexts and can't find an answer to the following questions. 我试图适应iOS的CGContextRef图形上下文,并且找不到以下问题的答案。

When you create a bitmap context with CGBitmapContextCreate() , you have an option to pass either a pre-allocated buffer or NULL in its data parameter. 使用CGBitmapContextCreate()创建位图上下文时,可以选择在其data参数中传递预分配的缓冲区或NULL In the second case: 在第二种情况下:

Pass NULL if you want this function to allocate memory for the bitmap. 如果要此函数为位图分配内存,则传递NULL。 This frees you from managing your own memory, which reduces memory leak issues. 这使您无需管理自己的内存,从而减少了内存泄漏问题。

Ok, that seems to be a good approach to adopt. 好的,这似乎是采用的好方法。 But what Apple's documentation lacks is a clear explanations of the following moments: 但是,Apple的文档缺少以下内容的清晰说明:

  • When does the data buffer get freed in the case you passed NULL to CGBitmapContextCreate() ? NULL传递给CGBitmapContextCreate()的情况下, 何时何时释放data缓冲区? Is it correct that it happens when you release the context with CGContextRelease() ? 使用CGContextRelease()释放上下文时是否发生这种情况是否正确?
  • And as opposed, when you passed a pointer to a pre-allocated buffer to CGBitmapContextCreate() , is it safe to suppose that after calling CGContextRelease() , you still have valid (not corrupted in some way) bitmap in your buffer? 相反,当您将指向预先分配的缓冲区的指针传递给CGBitmapContextCreate()可以安全地假设在调用CGContextRelease() ,缓冲区中仍然有有效的(未以某种方式损坏的)位图吗? Or you must not use the generated bitmap after the CGContextRelease() call? 还是在CGContextRelease()调用之后一定不能使用生成的位图?

Could someone experienced share their knowledge on this matter? 有经验的人可以在这个问题上分享他们的知识吗?

If you didn't create the buffer (passed NULL), it will get released by it's creator when you release the context.. 如果您没有创建缓冲区(传递给NULL),则在释放上下文时它将由其创建者释放。

If you create and pass a buffer, you're responsible for freeing it after you release the context. 如果创建并传递了缓冲区,则您有责任在释放上下文后释放它。 The context is only an abstraction for writing and reading that buffer, and releasing it doesn't affect the data in your buffer, your bitmap data is still in the buffer. 上下文只是用于写入和读取该缓冲区的抽象,释放上下文不会影响缓冲区中的数据,因为位图数据仍在缓冲区中。 You should be able to verify this easily by making a second bitmap context out of the buffer and saving it to a file for example. 例如,您应该能够通过在缓冲区之外创建第二个位图上下文并将其保存到文件中来轻松验证这一点。

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

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