开发者

Converting NSNumber to float is throwing me "uncaught exception 'NSInvalidArgumentException'"

I am trying to take EXIF data from the iPhone to calculate brightness. I need to access two specific NSNumber's ExifExposureTime and ExifISOSpeed to be converted to floats, but when I try to convert them to floats I am getting this error:

"2011-04-21 17:38:31.776 POP[11910:207] -[__NSCFArray floatValue]: unrecognized selector sent to instance 0x4b48f70 2011-04-21 17:38:31.777 POP[11910:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray floatValue]: unrecognized selector sent to instance 0x4b48f70' "

Is there some silly mistake that I am missing? Please let me know. Below is my code:

-(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*)kCGImagePropertyExifISOSpeed开发者_Python百科Ratings];

    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];
}

Here is what outputs in those 5 NSLOG statements to show that there are valid values stored:

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
) 

UPDATE: I was looking at my outputs and noticed the ExifISOSpeed is printing in that weird:

(
640
)

format. That was what the problem was when I was converting to a float, but would anyone know why it is outputting in such a fashion? I am able to use if statements and determine if it is greater than 0 so I am able to see it as a number.


It looks like ExifISOSpeed is an NSArray, not an NSNumber. I'd refer back to the documentation; maybe there's a reason you're getting an array back from the dictionary.

Try this:

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


ExifISOSpeed is an NSArray and not a NSNumber.

Try:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜