Objective c 检查是否支持UIGraphicsBeginImageContextWithOptions

Objective c 检查是否支持UIGraphicsBeginImageContextWithOptions,objective-c,cocoa-touch,uikit,ios4,retina-display,Objective C,Cocoa Touch,Uikit,Ios4,Retina Display,我正在开发一个iOS应用程序。它目前只在iOS4上工作,因为我在几种情况下使用了以下方法:“UIGraphicsBeginImageContextWithOptions”。此方法仅在iOS 4中可用,因此我的应用程序目前在iPhone OS 3上崩溃/无法工作。除了这个方法之外,没有理由不让这个应用程序在iPhoneOS3上运行。如何检查此方法是否可用?我尝试了以下方法但没有成功: if([self respondsToSelector:@selector(UIGraphicsBeginImag

我正在开发一个iOS应用程序。它目前只在iOS4上工作,因为我在几种情况下使用了以下方法:“UIGraphicsBeginImageContextWithOptions”。此方法仅在iOS 4中可用,因此我的应用程序目前在iPhone OS 3上崩溃/无法工作。除了这个方法之外,没有理由不让这个应用程序在iPhoneOS3上运行。如何检查此方法是否可用?我尝试了以下方法但没有成功:

if([self respondsToSelector:@selector(UIGraphicsBeginImageContextWithOptions)]) {
    UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0.0); // this will crop
}
else 
{
    UIGraphicsBeginImageContext(targetSize);

}
我只尝试过这样的变化:

if([self respondsToSelector:@selector(UIGraphicsBeginImageContextWithOptions:size:opaque:scale:)])


没有成功。任何帮助都将不胜感激。

UIGraphicsBeginImageContextWithOptions是一个C函数,因此您不能使用像
-respondsToSelector:
这样的Objective-C方法来测试它的存在性

但是,您可以打开UIKit框架,然后检查UIGraphicsBeginImageContextWithOptions是否为空:

if (UIGraphicsBeginImageContextWithOptions != NULL) {
   UIGraphicsBeginImageContextWithOptions(...);
} else {
   UIGraphicsBeginImageContext(...);
}

我也有同样的问题。您可以尝试测试系统版本。在我测试的设备上,这似乎对我有效

char majorVersion = [[[UIDevice currentDevice] systemVersion] characterAtIndex: 0]; if (majorVersion == '2' || majorVersion == '3') UIGraphicsBeginImageContext(...); else UIGraphicsBeginImageContextWithOptions(...); char majorVersion=[[UIDevice currentDevice]systemVersion]characterAtIndex:0]; 如果(主版本='2'| |主版本=='3') UIGraphicsBeginImageContext(…); 其他的 UIGraphicsBeginImageContextWithOptions(…);
我知道这是一个老问题,但对于新的Xcode和iOS版本(高于9),所有这些方法都适合我

我总是这样检查系统版本:

NSString *sysver = [[UIDevice currentDevice] systemVersion];
NSArray *versionNums = [sysver componentsSeparatedByString:@"."];
int majorVersion = [versionNums[0] intValue];
if (majorVersion > 3){
    UIGraphicsBeginImageContextWithOptions(...);
}
else{
    UIGraphicsBeginImageContext(...);
}

我希望这可以帮助任何人。

谢谢,你确定我需要削弱UIKit吗?@Gido:不。你可以使用
dlsym
。但是弱链接是最容易的。我削弱了UIKit框架,并输入了您在这里展示的代码。但是,在运行OS 3.0的iPhone上,它仍然能够通过IF语句而不是else语句。代码目前看起来是这样的:但是,在运行3.1.3的iPhone 3G上,代码最终出现在NSLog行中,该行没有此方法。我可能做错了什么?@Gigo:Try
NSLog(@“%p”,UIGraphicsBeginImageContextWithOptions)
NSString *sysver = [[UIDevice currentDevice] systemVersion];
NSArray *versionNums = [sysver componentsSeparatedByString:@"."];
int majorVersion = [versionNums[0] intValue];
if (majorVersion > 3){
    UIGraphicsBeginImageContextWithOptions(...);
}
else{
    UIGraphicsBeginImageContext(...);
}