从 UIImage 访问地理定位

Access Gelocation from UIImage

我可以从ios上的相册获取位置信息,但我想在拍照时访问图像位置(经纬度)。

所以这是图像完成事件

  public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)
    {
        //UIImagePickerController.ReferenceUrl
        var originalImage = new NSString("UIImagePickerControllerOriginalImage");
        var image = (UIImage)info[originalImage];
                foreach (var item in info)
                Console.WriteLine(item.Key + "  " + item.Value);


        image.SaveToPhotosAlbum(delegate(UIImage img, NSError err)
        {
            Console.WriteLine("Saved!");

        });


        _dp.DismissModalViewController(true);

        OnImageChoosed(image);
    }
} 

并且在信息字典中,我可以看到那些属性

UIImagePickerControllerMediaMetadata  {
    DPIHeight = 72;
    DPIWidth = 72;
    Orientation = 6;
    "{Exif}" =     {
        ApertureValue = "2.27500704749987";
        BrightnessValue = "5.267792568433492";
        ColorSpace = 1;
        DateTimeDigitized = "2015:07:08 14:24:15";
        DateTimeOriginal = "2015:07:08 14:24:15";
        ExposureBiasValue = 0;
        ExposureMode = 0;
        ExposureProgram = 2;
        ExposureTime = "0.008333333333333333";
        FNumber = "2.2";
        Flash = 24;
        FocalLenIn35mmFilm = 29;
        FocalLength = "4.15";
        ISOSpeedRatings =         (
            40
        );
        LensMake = Apple;
        LensModel = "iPhone 6 back camera 4.15mm f/2.2";
        LensSpecification =         (
            "4.15",
            "4.15",
            "2.2",
            "2.2"
        );
        MeteringMode = 5;
        PixelXDimension = 3264;
        PixelYDimension = 2448;
        SceneType = 1;
        SensingMethod = 2;
        ShutterSpeedValue = "6.908853996279415";
        SubjectArea =         (
            1631,
            1223,
            1795,
            1077
        );
        SubsecTimeDigitized = 927;
        SubsecTimeOriginal = 927;
        WhiteBalance = 0;
    };
    "{MakerApple}" =     {
        1 = 2;
        14 = 0;
        2 = <af001d01 9001ed01 9e011f01 70001500 41000d00 0a001000 12000900 0e000e00 b5003201 ae01f701 9b010401 5b001400 47002300 11001500 15000800 0d000d00 c2005501 eb014402 c901d900 67002400 3d002400 0f000b00 08000900 0d000d00 f1009201 30028e02 ff01b200 51000e00 43000e00 0d000e00 0d000d00 0c000b00 2d02b302 f802e902 26029700 80005600 5e000e00 0b000a00 09000900 0a000900 88039703 87033503 3c028300 44001100 45001100 0c000d00 0d000d00 0d000a00 b903b103 97034703 44027700 3e001000 38001000 0d000d00 0e000d00 0d000a00 a4039f03 87031503 22026d00 38000b00 34001000 0d001000 12000b00 0b000900 7b036a03 1d039c02 e2016400 37000e00 32001100 0d000a00 0a000600 07000700 c0029a02 4e02fb01 79015b00 36000e00 31001200 0c000900 0a000700 0c000b00 8b017801 4b013501 01015500 34000a00 30000900 06000c00 0e000800 14001000 b9001a00 34008a00 bd005000 2f000c00 2f000c00 0c000b00 0d000500 07000700 60000e00 0c002400 8f004b00 2f000a00 2d000f00 0e000700 07000700 0b000a00 47000b00 0a000b00 3d003300 28000a00 2b000a00 06000a00 0e000700 12001000 29000a00 09000a00 25003b00 2a000e00 2a000c00 0b000e00 11000700 0a000c00 27000a00 08000a00 0f003f00 3f001b00 31000d00 0e000700 07000700 0a000800>;
        3 =         {
            epoch = 0;
            flags = 1;
            timescale = 1000000000;
            value = 401611073225666;
        };
        4 = 1;
        5 = 128;
        6 = 119;
        7 = 1;
        8 =         (
            "0.01929908",
            "-0.713904",
            "-0.7083461"
        );
    };
    "{TIFF}" =     {
        DateTime = "2015:07:08 14:24:15";
        Make = Apple;
        Model = "iPhone 6";
        Software = "8.3";
        XResolution = 72;
        YResolution = 72;
    };

如您所见,没有位置信息。好吧,我尝试在获取位置信息后将图像保存在本地,但没有成功。

据我了解,如果我能得到图片保存到相册的路径,我就能得到

var path = args.Info[UIImagePickerController.ReferenceUrl].ToString();

refeenceURL 像上面一样,在我可以得到位置之后..但是我不知道如何得到新保存的图像的路径。

P.S.: 我在 C# 中使用了 Xamarin.iOS 但如果你提供 objective C 或 swift 代码并不重要。

我有一个我从未使用过的旧片段(属于 NSMutableDictionary 类别),因为我在应用程序中使用了 AVFoundation,但值得一试。感谢该代码的创建者 Gustavo Ambrozio。
因为真的很旧,你应该适应 ARC 兼容性。

- (id)initWithInfoFromImagePicker:(NSDictionary *)info {

    if ((self = [self init])) {

        // Key UIImagePickerControllerReferenceURL only exists in iOS 4.1
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.1f) {

            NSURL* assetURL = nil;
            if ((assetURL = [info objectForKey:UIImagePickerControllerReferenceURL])) {

                ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
                [library assetForURL:assetURL 
                         resultBlock:^(ALAsset *asset)  {
                             NSDictionary *metadata = asset.defaultRepresentation.metadata;
                             [self addEntriesFromDictionary:metadata];
                         }
                        failureBlock:^(NSError *error) {
                        }];
                [library autorelease];
            }
            else {
                NSDictionary *metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
                if (metadata)
                    [self addEntriesFromDictionary:metadata];
            }
        }
    }

    return self;
}