[英]Objective-C: How to check if a C function is supported
如何執行運行時檢查以查看是否可以使用UIGraphicsBeginImageContextWithOptions
,它僅從 iOS 4 開始可用。
我知道我可以檢查[[UIDevice currentDevice] systemVersion]
,但 Apple 建議使用NSClassFromString()
或respondsToSelector:
之類的東西。 是否有respondsToSelector:
對於 C 功能?
這是我一直在使用的另一個選項。
C 函數是指針。 If you "weak" link to UIKit
framework, on iOS 3 the function pointer will simply be NULL
, so you can test for the existence of the function by doing:
if (UIGraphicsBeginImageContextWithOptions)
{
// On iOS 4+, use the main screen's native scale factor (for iPhone 4).
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
}
else
{
UIGraphicsBeginImageContext(size);
}
另請參閱: 如何在 Xcode 4 上弱鏈接框架?
您可能在這里感興趣的是弱鏈接。 (參見“清單 3-2:檢查 C 函數的可用性”。)
這就是我要做的:
if ([mainScreen respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(newSize, NO, [[UIScreen mainScreen] scale]);
} else {
UIGraphicsBeginImageContext(newSize);
}
我認為這已經足夠好了,但是如果您有更好的建議,請回答。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.