CGImageCreateCopy vs CGImageRetain

A CGImageRef does not seem to have any mutating operations. Copying CGImageRef does not copy CGDataProviderRef (determined by experimentation). Therefore, if CGImageRef is an immutable type, is there a reason for copy vs. retain ?

+4
source share
2 answers

A CGImageRef does not have any mutating operations

There are no public functions to modify CGImage , however this does not mean that CGImageRef is immutable. CGImageRef opaque, that is, the internal structure is not documented, but there are ways to change it.

 CGImageRef imageRef = ... struct CGImage image = *imageRef; //if you know the internal structure, you can do things like image.provider = ... 

I assume that Core Graphics often creates internal copies of an image, for example CGImageCreateCopyWithColorSpace , perhaps uses CGImageCreateCopy internally. Copies can also be made when the image is drawn.

And, of course, this is a very good design for โ€œsubclassingโ€ (not subclasses, since we have structures, not objects).

+2
source

No. Sometimes you need copy , for example, you can get CGImageRef support for a UIImage , but it would be bad practice to try and retain it - you would make copy in this case.

0
source

Source: https://habr.com/ru/post/1482311/


All Articles