Saving Geotag info with photo on iOS4.1

后端 未结 6 1344
旧时难觅i
旧时难觅i 2020-12-01 03:21

I am having major issues trying to save a photo to camera roll with geotag info on iOS4.1. I am using following ALAssetsLibrary API:

- (void)writeImageDataTo         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 03:47

    Class to Write GPS Data to Meta-Data.

    class GeoTagImage {
    
      /// Writes GPS data into the meta data.
      /// - Parameters:
      ///   - data: Coordinate meta data will be written to the copy of this data.
      ///   - coordinate: Cooordinates to write to meta data.
      static func mark(_ data: Data, with coordinate: Coordinate) -> Data {
        var source: CGImageSource? = nil
        source = CGImageSourceCreateWithData((data as CFData?)!, nil)
        // Get all the metadata in the image
        let metadata = CGImageSourceCopyPropertiesAtIndex(source!, 0, nil) as? [AnyHashable: Any]
        // Make the metadata dictionary mutable so we can add properties to it
        var metadataAsMutable = metadata
        var EXIFDictionary = (metadataAsMutable?[(kCGImagePropertyExifDictionary as String)]) as? [AnyHashable: Any]
        var GPSDictionary = (metadataAsMutable?[(kCGImagePropertyGPSDictionary as String)]) as? [AnyHashable: Any]
    
        if !(EXIFDictionary != nil) {
          // If the image does not have an EXIF dictionary (not all images do), then create one.
          EXIFDictionary = [:]
        }
        if !(GPSDictionary != nil) {
          GPSDictionary = [:]
        }
    
        // add coordinates in the GPS Dictionary
        GPSDictionary![(kCGImagePropertyGPSLatitude as String)] = coordinate.latitude
        GPSDictionary![(kCGImagePropertyGPSLongitude as String)] = coordinate.longitude
        EXIFDictionary![(kCGImagePropertyExifUserComment as String)] = "Raw Image"
    
        // Add our modified EXIF data back into the image’s metadata
        metadataAsMutable!.updateValue(GPSDictionary!, forKey: kCGImagePropertyGPSDictionary)
        metadataAsMutable!.updateValue(EXIFDictionary!, forKey: kCGImagePropertyExifDictionary)
    
        // This is the type of image (e.g., public.jpeg)
        let UTI: CFString = CGImageSourceGetType(source!)!
    
        // This will be the data CGImageDestinationRef will write into
        let dest_data = NSMutableData()
        let destination: CGImageDestination = CGImageDestinationCreateWithData(dest_data as CFMutableData, UTI, 1, nil)!
        // Add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
        CGImageDestinationAddImageFromSource(destination, source!, 0, (metadataAsMutable as CFDictionary?))
    
        // Tells the destination to write the image data and metadata into our data object.
        // It will return false if something goes wrong
        _ = CGImageDestinationFinalize(destination)
    
        return (dest_data as Data)
      }
    
      /// Prints the Meta Data from the Data.
      /// - Parameter data: Meta data will be printed of this object.
      static func logMetaData(from data: Data) {
        if let imageSource = CGImageSourceCreateWithData(data as CFData, nil) {
          let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)
          if let dict = imageProperties as? [String: Any] {
            print(dict)
          }
        }
      }
    }
    

提交回复
热议问题