How can I get the contents of an std::string into

2019-01-26 22:37发布

问题:

I've got a function that returns a std::string object. I'm working with Cocoa/CoreGraphics and I need a way to get the data from that string into a CFData object so that I can feed that into a CGDataProviderCreateWithCFData object to make a CGImage.

The CreateCFData function wants a const UInt8* object (UInt8 being a typedef for unsigned char). The string represents the bytes from a decoded Base64 string (image data), so it appears to contain many null "characters" so the obvious casting of the .c_str() output to an unsigned char* object won't work.

I'm less experienced with C++ and very new to Cocoa/CoreGraphics, so if there's a much better way to accomplish what I'm wanting to do please let me know.

回答1:

CFDataCreate( NULL, (const UInt8*) myString.data(), myString.size() )