Convert or Print CGPDFStringRef string

坚强是说给别人听的谎言 提交于 2019-12-07 11:55:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!