将NSNumber转换为float会导致“未捕获的异常'NSInvalidArgumentException'”

时间:2011-04-21 21:57:50

标签: iphone floating-point exif nsnumber

我正在尝试从iPhone获取EXIF数据来计算亮度。我需要访问两个特定的NSNumber的ExifExposureTime和ExifISOSpeed才能转换为浮点数,但当我尝试将它们转换为浮点数时,我收到此错误:

“2011-04-21 17:38:31.776 POP [11910:207] - [__ NSCFArray floatValue]:无法识别的选择器发送到实例0x4b48f70 2011-04-21 17:38:31.777 POP [11910:207] * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSCFArray floatValue]:无法识别的选择器发送到实例0x4b48f70' “

我遗失了一些愚蠢的错误吗?请告诉我。以下是我的代码:

-(IBAction)getDataOne:(id)sender {
    NSString *aPath = [[NSBundle mainBundle] pathForResource:@"IMG_0062" ofType:@"JPG"];

    NSURL *url = [NSURL fileURLWithPath:aPath];

    CGImageSourceRef sourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);

    NSDictionary *immutableMetadata = (NSDictionary *) CGImageSourceCopyPropertiesAtIndex(sourceRef,0,NULL);

    NSDictionary *exifDic = [immutableMetadata objectForKey:(NSString *)kCGImagePropertyExifDictionary];

    NSNumber *ExifApertureValue  = [exifDic objectForKey:(NSString*)kCGImagePropertyExifApertureValue];
    NSNumber *ExifShutterSpeed  = [exifDic objectForKey:(NSString*)kCGImagePropertyExifShutterSpeedValue];
    NSNumber *ExifExposureTime  = [exifDic objectForKey:(NSString*)kCGImagePropertyExifExposureTime];
    NSNumber *ExifFStop  = [exifDic objectForKey:(NSString*)kCGImagePropertyExifFNumber];
    NSNumber *ExifISOSpeed  = [exifDic objectForKey:(NSString*)kCGImagePropertyExifISOSpeedRatings];

    NSLog(@"ExifApertureValue : %@ \n",ExifApertureValue);
    NSLog(@"ExifShutterSpeed : %@ \n",ExifShutterSpeed);
    NSLog(@"ExifExposureTime : %@ \n",ExifExposureTime);
    NSLog(@"ExifFStop : %@ \n",ExifFStop);
    NSLog(@"ExifISOSpeed : %@ \n",ExifISOSpeed);

    float brightness, T, ISO;
    float K = 12.0;
    float A2 = 7.84;

    T = [ExifExposureTime floatValue];
    ISO = [ExifISOSpeed floatValue];

    brightness = (A2 * K) / (T * ISO);

    [summaryViewController imageOneSuccess];

    [ExifApertureValue release];
    [ExifShutterSpeed release];
    [ExifExposureTime release];
    [ExifFStop release];
    [ExifISOSpeed release];
}

以下是这5个NSLOG语句中的输出,表明存储了有效值:

2011-04-21 18:05:12.318 POP[12051:207] ExifApertureValue : 2.526069 
2011-04-21 18:05:12.319 POP[12051:207] ExifShutterSpeed : 4.915926 
2011-04-21 18:05:12.321 POP[12051:207] ExifExposureTime : 0.03333334 
2011-04-21 18:05:12.323 POP[12051:207] ExifFStop : 2.4 
2011-04-21 18:05:12.324 POP[12051:207] ExifISOSpeed : (
    640
) 

更新:我正在查看我的输出,并注意到ExifISOSpeed打印的很奇怪:

(
640
)

格式。这就是我转换成浮动时的问题,但有人知道为什么它会以这种方式输出吗?我能够使用if语句并确定它是否大于0所以我能够将其视为数字。

2 个答案:

答案 0 :(得分:2)

看起来ExifISOSpeedNSArray,而不是NSNumber。我会回顾文档;也许有一个原因让你从字典中得到一个数组。

试试这个:

ISO = [(NSNumber *)[ExifISOSpeed objectAtIndex:0] floatValue];

答案 1 :(得分:1)

ExifISOSpeedNSArray而不是NSNumber

尝试:

NSNumber *ExifISOSpeed  = [[exifDic objectForKey:(NSString*)kCGImagePropertyExifISOSpeedRatings] objectAtIndex:0];