【问题标题】:Accessing Image Properties Without Loading the Image Into Memory在不将图像加载到内存的情况下访问图像属性
【发布时间】:2018-05-26 13:34:39
【问题描述】:

我正在尝试从 UIImagePickerController 中挑选的图像中获取 GPS 数据

我使用this site 作为参考。

选择图片后,CGImageSourceRef 始终为空。

这是我的 NSLog 的输出

信息:{ UIImagePickerControllerMediaType = "public.image"; UIImagePickerControllerOriginalImage = "尺寸 {2448, 3264} 方向 3 比例 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

}

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

【问题讨论】:

    标签: ios objective-c uiimagepickercontroller


    【解决方案1】:

    UIImagePickerControllerReferenceURL 已弃用。 You should use UIImagePickerControllerPHAsset instead. 假设 info 字典中有该键的 PHAsset,它还应该提供 location data as a CLLocation.

    编辑:既然你说你需要支持早期的操作系统版本,那么这段代码对我来说是基于你已经拥有的。

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
      [picker dismissViewControllerAnimated:YES completion:^{
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
          NSURL *imageFileURL = info[UIImagePickerControllerImageURL];
          CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageFileURL, NULL);
    
          if (imageSource == NULL) {
            NSLog(@"ImageSource was Null");
            return;
          }
    
          NSDictionary *options = @{(NSString *)kCGImageSourceShouldCache : @(NO)};
          CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
          CFRelease(imageSource);
    
          CFDictionaryRef gps = CFDictionaryGetValue(imageProperties, kCGImagePropertyGPSDictionary);
          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);
        });//background queue
      }];//picker completion
    }
    

    【讨论】:

    • 不幸的是,我必须支持回到 iOS9,而这仅适用于 iOS 11。
    • 这太糟糕了。我使用与您的原始方法接近的方法用我的解决方案更新了答案。我使用的 URL 是 UIImagePickerControllerImageURL 而不是 UIImagePickerControllerReferenceURL。还有一些语法糖和免费桥接。
    • 已弃用并不意味着它不起作用。这仅意味着该 API 计划在不久的将来停止使用。所以在 iOS11 中使用 UIImagePickerControllerReferenceURL 就可以了。
    • 对不起@Dare 这仍然不起作用。 ImageSource 仍然是null
    猜你喜欢
    • 1970-01-01
    • 2010-12-05
    • 2015-09-17
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    相关资源
    最近更新 更多