访问图像属性而不将图像加载到内存中

时间:2017-12-12 19:07:15

标签: ios objective-c uiimagepickercontroller

我正在尝试从UIImagePickerController

中挑选的图像中获取 GPS 数据

我使用this site作为参考。

选择所选图像后,CGImageSourceRef始终为空。

这是我的NSLog的输出

  

信息:{        UIImagePickerControllerMediaType =“public.image”;        UIImagePickerControllerOriginalImage =“size {2448,3264} orientation 3 scale 1.000000”;       UIImagePickerControllerReferenceURL =“assets-library://asset/asset.JPG?id = B01DF2C7-B954-41D8-B3FE-6096706DF3BB& ext = JPG”;    }

     

imageFileURL:assets-library://asset/asset.JPG?id = B01DF2C7-B954-41D8-B3FE-6096706DF3BB& ext = JPG

     

ImageSource是空的

这是我的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSLog(@"Info: %@", info.description);


    [picker dismissViewControllerAnimated:YES completion:^{


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            NSURL *imageFileURL = (NSURL*) info[UIImagePickerControllerReferenceURL];

            NSLog(@"imageFileURL: %@", imageFileURL.description);

            CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef) imageFileURL, NULL);

            if (imageSource == NULL) {
                NSLog(@"ImageSource was Null");
                return;
            }

            NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,
                                    nil];
            CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
            CFRelease(imageSource);

            CFDictionaryRef gps = CFDictionaryGetValue(imageProperties, kCGImagePropertyGPSDictionary);
            if (gps) {
                NSString *latitudeString = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitude);
                NSString *latitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitudeRef);
                NSString *longitudeString = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitude);
                NSString *longitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitudeRef);
                NSLog(@"GPS Coordinates: %@ %@ / %@ %@", longitudeString, longitudeRef, latitudeString, latitudeRef);
            }

            dispatch_async(dispatch_get_main_queue(), ^{

            }); //distpach get main queue
        });//background queueu
    }];//picker completion

}

我不确定我做错了什么。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

不推荐使用UIImagePickerControllerReferenceURL。 You should use UIImagePickerControllerPHAsset instead.假设信息词典中有该密钥的PHAsset,它还应该提供location data as a CLLocation.

编辑:由于您说您需要支持早期的操作系统版本,因此该代码基于您已有的功能为我工作。

Iterator bodyParts = jsonObject.keys();
while (bodyParts.hasNext()){
    String bodyPart = (String) bodyParts.next();
    JSONObject symptomsJson = jsonObject.getJSONObject(bodyPart);
    Iterator symptoms = symptomsJson.keys();
    // And so on...
}