美文网首页
iOS图片库照片GPS处理

iOS图片库照片GPS处理

作者: 左方 | 来源:发表于2020-07-07 10:26 被阅读0次

    最近想对图片库里的拍照地点进行批量添加。
    定位和获取图片库照片比较好解决。
    但是添加地理位置信息就麻烦多了。

    单反拍的照片传输进手机之后,是没有地理位置信息的。
    找了网上的教程,获取照片地址信息,发现根本获取不到:

    NSData *imageData = UIImageJPEGRepresentation(获取的照片, 1);
            CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
            NSDictionary *imageInfo = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
            NSMutableDictionary *metaDataDic = [imageInfo mutableCopy];
            NSMutableDictionary *exifDic =[[metaDataDic objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
            NSMutableDictionary *GPSDic =[[metaDataDic objectForKey:(NSString*)kCGImagePropertyGPSDictionary]mutableCopy];
    

    单反拍摄,没有GPS信息的照片返回:

    {
    ColorModel = RGB;
    Depth = 8;
    Orientation = 1;
    PixelHeight = 1244;
    PixelWidth = 828;
    ProfileName = "sRGB IEC61966-2.1";
    "{Exif}" = {
    ColorSpace = 1;
    PixelXDimension = 828;
    PixelYDimension = 1244;
    };
    "{JFIF}" = {
    DensityUnit = 0;
    JFIFVersion = (
    1,
    0,
    1
    );
    XDensity = 72;
    YDensity = 72;
    };
    "{TIFF}" = {
    Orientation = 1;
    };
    }

    iPhone拍摄,有地理位置信息的照片:

    {
    ColorModel = RGB;
    Depth = 8;
    Orientation = 1;
    PixelHeight = 1102;
    PixelWidth = 826;
    ProfileName = "Display P3";
    "{Exif}" = {
    PixelXDimension = 826;
    PixelYDimension = 1102;
    };
    "{JFIF}" = {
    DensityUnit = 0;
    JFIFVersion = (
    1,
    0,
    1
    );
    XDensity = 72;
    YDensity = 72;
    };
    "{TIFF}" = {
    Orientation = 1;
    };
    }

    两种都没法获取位置信息。
    如果将位置信息写成字典

    [GPSDic setObject:[NSNumber numberWithFloat:37.795]
    forKey:(NSString)kCGImagePropertyGPSLatitude];
    [GPSDic setObject:@"N" forKey:(NSString
    )kCGImagePropertyGPSLatitudeRef];
    [GPSDic setObject:[NSNumber numberWithFloat:122.410]
    forKey:(NSString)kCGImagePropertyGPSLongitude];
    [GPSDic setObject:@"W" forKey:(NSString
    )kCGImagePropertyGPSLongitudeRef];
    NSLog(@"%@",GPSDic);

    强行加上,也无济于事。
    突然发现该照片的资源PHAsset信息里,有location属性,是可以拿到位置信息的。那么通过对PHAsset的操作是否可以添加GPS信息呢?

    1、
    //UIimage转换PHAsset
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                PHAssetChangeRequest * request = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
                if (request.placeholderForCreatedAsset.localIdentifier) {
                    NSString *localIndentifier = request.placeholderForCreatedAsset.localIdentifier;
                    PHFetchResult *assetResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIndentifier] options:nil];
                    PHAsset *asset = assetResult.firstObject;
                }
            } completionHandler:^(BOOL success, NSError * _Nullable error) {
                if (success) {
                    NSLog(@"添加成功!");
                } else {
                    NSLog(@"添加失败!");
                }
            }];
    
    2、从PHAsset中获取更详细的照片信息:

    有地理位置信息的照片{
    ColorModel = RGB;
    DPIHeight = 72;
    DPIWidth = 72;
    Depth = 8;
    Orientation = 6;
    PixelHeight = 3024;
    PixelWidth = 4032;
    PrimaryImage = 1;
    ProfileName = "Display P3";
    "{Exif}" = {
    ApertureValue = "1.69599381283836";
    BrightnessValue = "2.559109103158562";
    ColorSpace = 65535;
    ComponentsConfiguration = (
    1,
    2,
    3,
    0
    );
    DateTimeDigitized = "2020:07:06 15:06:48";
    DateTimeOriginal = "2020:07:06 15:06:48";
    ExifVersion = (
    2,
    3,
    1
    );
    ExposureBiasValue = 0;
    ExposureMode = 0;
    ExposureProgram = 2;
    ExposureTime = "0.05882352941176471";
    FNumber = "1.8";
    Flash = 16;
    FlashPixVersion = (
    1,
    0
    );
    FocalLenIn35mmFilm = 28;
    FocalLength = 4;
    ISOSpeedRatings = (
    40
    );
    LensMake = Apple;
    LensModel = "iPhone X back dual camera 4mm f/1.8";
    LensSpecification = (
    4,
    6,
    "1.8",
    "2.4"
    );
    MeteringMode = 5;
    OffsetTime = "+08:00";
    OffsetTimeDigitized = "+08:00";
    OffsetTimeOriginal = "+08:00";
    PixelXDimension = 4032;
    PixelYDimension = 3024;
    SceneCaptureType = 0;
    SceneType = 1;
    SensingMethod = 2;
    ShutterSpeedValue = "4.059350611812313";
    SubjectArea = (
    2015,
    1511,
    2217,
    1330
    );
    SubsecTimeDigitized = 139;
    SubsecTimeOriginal = 139;
    WhiteBalance = 0;
    };
    "{GPS}" = {
    Altitude = "4.610363961252704";
    AltitudeRef = 0;
    DestBearing = "60.45497512437811";
    DestBearingRef = T;
    HPositioningError = "66.42636786961583";
    ImgDirection = "60.45497512437811";
    ImgDirectionRef = T;
    Latitude = "xx.xxxxxxxxxxxx";
    LatitudeRef = N;
    Longitude = "xx.xxxxxxxxxxxx";
    LongitudeRef = E;
    Speed = 0;
    SpeedRef = K;
    };
    "{MakerApple}" = {
    1 = 11;
    12 = (
    "53.55469",
    "95.67578"
    );
    13 = 26;
    14 = 0;
    15 = 2;
    16 = 1;
    2 = {length = 512, bytes = 0x88008500 83008600 87008d00 90009200 ... c0004f00 7000b400 };
    20 = 5;
    23 = 0;
    25 = 0;
    26 = q825s;
    3 = {
    epoch = 0;
    flags = 1;
    timescale = 1000000000;
    value = 1645823363837291;
    };
    31 = 0;
    32 = "6DEA2F72-2701-47E0-804C-78F183EEC448";
    33 = 0;
    35 = (
    12,
    536871070
    );
    37 = 0;
    38 = 0;
    39 = 0;
    4 = 1;
    43 = "A00C17E9-F64B-46D0-B7FF-BE48307967CF";
    5 = 174;
    6 = 185;
    7 = 1;
    8 = (
    "0.1065291",
    "0.01893699",
    "-0.9845731"
    );
    };
    "{TIFF}" = {
    DateTime = "2020:07:06 15:06:48";
    Make = Apple;
    Model = "iPhone X";
    Orientation = 6;
    ResolutionUnit = 2;
    Software = "13.5.1";
    TileLength = 512;
    TileWidth = 512;
    XResolution = 72;
    YResolution = 72;
    };
    }

    照片信息比从UIImage中取得的详细多了。

    3、更新图片和视频属性(名义上是更新,但是也只能更新四个内容:创建日期、拍照地点、是否收藏、是否隐藏。)
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest * changeRequest = [PHAssetChangeRequest changeRequestForAsset:asset];
        changeRequest.location = self.location;
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
      if (success) {
        NSLog(@"修改成功");
      } else {
        NSLog(@"修改失败");
      }
    }];
    

    参考资料:
    https://www.jianshu.com/p/40f3c6e02061 PHAsset的增删改查

    相关文章

      网友评论

          本文标题:iOS图片库照片GPS处理

          本文链接:https://www.haomeiwen.com/subject/onooqktx.html