ios - 从 iOS iphone 中相机返回的图像中读取 GPS 数据

标签 ios geolocation gps camera

我需要获取使用 iOS 设备的相机拍摄的图像的 GPS 坐标。我不关心相机胶卷图像,只关心用 UIImagePickerControllerSourceTypeCamera 拍摄的图像。

我读过很多 stackoverflow 的答案,比如 Get Exif data from UIImage - UIImagePickerController ,它假设您使用的是 AssetsLibrary 框架,它似乎不适用于相机图像,或者使用 CoreLocaiton 从应用程序本身而不是图像中获取纬度/经度。

使用 CoreLocation 不是一种选择。按下快门按钮时不会给我坐标。 (使用基于 CoreLocation 的解决方案,您需要在调出相机 View 之前或之后记录坐标,当然,如果设备正在移动,坐标将是错误的。此方法适用于固定设备。)

我只有 iOS5,所以我不需要支持旧设备。这也适用于商业产品,所以我不能使用 http://code.google.com/p/iphone-exif/ .

那么,从 iOS5 中的相机返回的图像中读取 GPS 数据的选项有哪些?我现在能想到的就是将图像保存到相机胶卷,然后使用 AssetsLibrary,但这似乎很不靠谱。

谢谢!


这是我根据 Caleb 的回答编写的代码。

    UIImage *image =  [info objectForKey:UIImagePickerControllerOriginalImage];

    NSData *jpeg = UIImageJPEGRepresentation(image,1.0);
    CGImageSourceRef  source ;
    source = CGImageSourceCreateWithData((__bridge CFDataRef)jpeg, NULL);

    NSDictionary *metadataNew = (__bridge NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source,0,NULL);  

    NSLog(@"%@",metadataNew);

并且我的控制台显示:

    2012-04-26 14:15:37:137 ferret[2060:1799] {
        ColorModel = RGB;
        Depth = 8;
        Orientation = 6;
        PixelHeight = 1936;
        PixelWidth = 2592;
        "{Exif}" =     {
            ColorSpace = 1;
            PixelXDimension = 2592;
            PixelYDimension = 1936;
        };
        "{JFIF}" =     {
            DensityUnit = 0;
            JFIFVersion =         (
                1,
                1
            );
            XDensity = 1;
            YDensity = 1;
        };
        "{TIFF}" =     {
            Orientation = 6;
        };
    }

没有纬度/经度。

最佳答案

问题是,自从 iOS 4 UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 剥离了地理定位。要解决此问题,您必须使用原始照片路径来访问完整的图像元数据。像这样:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:referenceURL resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        NSDictionary *metadata = rep.metadata;
        NSLog(@"%@", metadata);

        CGImageRef iref = [rep fullScreenImage] ;

        if (iref) {
            self.imageView.image = [UIImage imageWithCGImage:iref];
        }
    } failureBlock:^(NSError *error) {
        // error handling
    }];

输出应该是这样的:

{
    ColorModel = RGB;
    DPIHeight = 72;
    DPIWidth = 72;
    Depth = 8;
    Orientation = 6;
    PixelHeight = 1936;
    PixelWidth = 2592;
    "{Exif}" =     {
        ApertureValue = "2.970854";
        BrightnessValue = "1.115874";
        ColorSpace = 1;
        ComponentsConfiguration =         (
            0,
            0,
            0,
            1
        );
        DateTimeDigitized = "2012:07:14 21:55:05";
        DateTimeOriginal = "2012:07:14 21:55:05";
        ExifVersion =         (
            2,
            2,
            1
        );
        ExposureMode = 0;
        ExposureProgram = 2;
        ExposureTime = "0.06666667";
        FNumber = "2.8";
        Flash = 24;
        FlashPixVersion =         (
            1,
            0
        );
        FocalLength = "3.85";
        ISOSpeedRatings =         (
            200
        );
        MeteringMode = 5;
        PixelXDimension = 2592;
        PixelYDimension = 1936;
        SceneCaptureType = 0;
        SensingMethod = 2;
        Sharpness = 2;
        ShutterSpeedValue = "3.9112";
        SubjectArea =         (
            1295,
            967,
            699,
            696
        );
        WhiteBalance = 0;
    };
    "{GPS}" =     {
        Altitude = "1167.528";
        AltitudeRef = 0;
        ImgDirection = "278.8303";
        ImgDirectionRef = T;
        Latitude = "15.8235";
        LatitudeRef = S;
        Longitude = "47.99416666666666";
        LongitudeRef = W;
        TimeStamp = "00:55:04.59";
    };
    "{TIFF}" =     {
        DateTime = "2012:07:14 21:55:05";
        Make = Apple;
        Model = "iPhone 4";
        Orientation = 6;
        ResolutionUnit = 2;
        Software = "5.1.1";
        XResolution = 72;
        YResolution = 72;
        "_YCbCrPositioning" = 1;
    };
}

关于ios - 从 iOS iphone 中相机返回的图像中读取 GPS 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10302250/

相关文章:

java - 我如何根据 GPS 位置在网络服务器和 Android 应用程序之间进行交互?

android - GPS 位置波动

iphone - 我应该在哪里存储图像,以便我可以使用 iTunes 文件共享它们?

ios - 禁用 UITextView 中的水平滚动

geolocation - Wikipedia API的地理位置

java - 如何在 freebase 中搜索建筑物附近的地点?

ios - 将 NSString 从一个类传递到另一个类。 (ECSlidingViewController?)

ios - iPhone ios 上的 swift 3 中的弹出窗口

ios - SQLite查询区域内的位置字段

javascript - 如何在 Node.js 中使用 GPS?