我有一段来自 GIT 上出色的 VFR PDF 查看器的 ObjC 代码。它CGPDFDictionaryGetString
用于从 PDF 注释中获取指向字符串的指针。然后它使用一些字节指针转换来获得最终的字符串。在 Monotouch 中,CGPDFDictionary.GetString()
只有一个.GetName()
- 这是唯一返回字符串的方法,所以我认为它必须是正确的方法,但它不起作用。我可以很好地检索数组、字典、浮点数和整数——只有字符串似乎不起作用。
请参阅下面的小代码示例。
CGPDFStringRef uriString = NULL;
// This returns TRUE in the ObjC version and uriString is a valid pointer to a string.
if (CGPDFDictionaryGetString(actionDictionary, "URI", &uriString) == true)
{
// Do some pointer magic - how to do this in MT? Do I have to at all?
const char *uri = (const char *)CGPDFStringGetBytePtr(uriString);
// *uri now contains a URL, I can see it in the debugger.
}
我是这样翻译的:
string sUri = null;
// This returns FALSE. Hence my sUri is NULL. Seems like GetName() is not the analogy to CGPDFDictionaryGetString.
if(oActionDic.GetName("URI", out sUri))
{
// I never get here.
}
编辑: 查看 Mono 源,我可以在 Master 分支中看到: // TODO: GetString -> 返回一个 CGPDFString
切换到分支 4.2 表明它似乎在那里。所以我从那里复制了代码,但有两个问题:
- 我收到有关“不安全”关键字的错误。它告诉我添加“不安全”命令行选项。那是什么,添加它是个好主意吗?在哪里?
- 它似乎仍然运行,但在获取 CGPDFString 时应用程序挂起。
[DllImport (Constants.CoreGraphicsLibrary)] public extern static IntPtr CGPDFStringGetLength (IntPtr pdfStr);
[DllImport (Constants.CoreGraphicsLibrary)]
public extern static IntPtr CGPDFStringGetBytePtr (IntPtr pdfStr);
public static string PdfStringToString (IntPtr pdfString)
{
if (pdfString == IntPtr.Zero)
return null;
int n = (int)CGPDFStringGetLength (pdfString);
unsafe
{
return new String ((char *)CGPDFStringGetBytePtr (pdfString), 0, n);
}
}
[DllImport (Constants.CoreGraphicsLibrary)]
extern static bool CGPDFDictionaryGetString (IntPtr handle, string key, out IntPtr result);
public static bool GetStringFromPdfDictionary (CGPDFDictionary oPdfDic, string key, out string result)
{
if (key == null)
throw new ArgumentNullException ("key");
IntPtr res;
if (CGPDFDictionaryGetString (oPdfDic.Handle, key, out res))
{
result = PdfStringToString (res);
return true;
}
result = null;
return false;
}