问题
How to convert a CGPDFStringRef to unicode char? I have used CGPDFStringCopyTextString to get the string and then [string characterAtIndex:i] to cast to unichar, is this the right way? or is there any way to get the bytes of the string and convert to unicode directly? Need some guidance here.
回答1:
NSString is capable of handling of unicode characters itself, you just need to convert the CGPDFString to NSString and further you can use it as follows:
NSString *tempStr = (NSString *)CGPDFStringCopyTextString(objectString);
回答2:
although UPT's answer is correct, it will produce a memory leak
from the documentation: CGPDFStringCopyTextString "...You are responsible for releasing this object."
the correct way to do this would be:
CFStringRef _res = CGPDFStringCopyTextString(pdfString);
NSString *result = [NSString stringWithString:(__bridge NSString *)_res];
CFRelease(_res);
回答3:
It's not a bad idea, even if you can access the CGPDFString directly using CGPDFStringGetBytePtr
. You will also need CGPDFStringGetLength
to get the string length, as it may not be null-terminated.
See the documentation for more info
来源:https://stackoverflow.com/questions/7802737/convert-or-print-cgpdfstringref-string