PHAsset AVAsset UIImage CIImage CGImage NSData 에서 위치 정보 가져오기 - kirseia/study GitHub Wiki

위치 정보 가져오기

  • 기본적으로는 exif 에 저장된 정보를 가져오는 것

PHAsset 의 경우

  • phAsset.location 으로 가져올 수 있음 (비디오, 이미지 모두 가능)

NSData 의 경우

  • (AVAsset/ UIImage, CIImage, CGImage, NSData 모두 공통)
  • data를 뒤져서 metadata를 가져오고, exif 에서 gps 정보를 확인해서 가져올 수 있음.
  • 비디오의 경우 exif 가 없어서 data에서 가져올 수 없다.

비디오의 경우

  • AVAsset으로 변환해서 metadata를 가져오면 값을 가져올 수 있음.
  • AVMetadataKey.commonKeyLocation 으로 위치 정보를 가져올 수 있음 (utf-8 스타일 string)
extension String {
    var toCLLocation: CLLocation? {
        get {
            if self.count < 17 {
                return nil
            }
            
            guard let latitude = Double(self.substring(from: 0, to: 8)) else {
                return nil
            }
            
            guard let longitude = Double(self.substring(from: 8, to: 17)) else {
                return nil
            }
            
            return CLLocation(latitude: latitude, longitude: longitude)
        }
        
    }
    
    func substring(from: Int, to: Int) -> String {
        let start = index(startIndex, offsetBy: from)
        let end = index(start, offsetBy: to - from)
        return String(self[start ..< end])
    }

}

으로 변환 가능

  • AVMetadataKey.commonKeyCreationDate 으로 날짜 정보를 가져올 수 있음 (utf-8 스타일 string)
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"

로 변환 가능

CLLocation 팁

metadata 내용

 ▿ 15 elements
 ▿ 0 : 2 elements
 - key : ImgDirection
 - value : 58.91798400595128
 ▿ 1 : 2 elements
 - key : LatitudeRef
 - value : N
 ▿ 2 : 2 elements
 - key : HPositioningError
 - value : 65
 ▿ 3 : 2 elements
 - key : DestBearingRef
 - value : M
 ▿ 4 : 2 elements
 - key : Latitude
 - value : 34.84813
 ▿ 5 : 2 elements
 - key : Speed
 - value : 0
 ▿ 6 : 2 elements
 - key : TimeStamp
 - value : 05:43:09 // 무조건 UTC가 들어감
 ▿ 7 : 2 elements
 - key : LongitudeRef
 - value : E
 ▿ 8 : 2 elements
 - key : AltitudeRef
 - value : 0
 ▿ 9 : 2 elements
 - key : Longitude
 - value : 127.34468
 ▿ 10 : 2 elements
 - key : Altitude
 - value : 11.90512752625541
 ▿ 11 : 2 elements
 - key : DateStamp
 - value : 2018:12:15
 ▿ 12 : 2 elements
 - key : DestBearing
 - value : 58.91798400595128
 ▿ 13 : 2 elements
 - key : ImgDirectionRef
 - value : M
 ▿ 14 : 2 elements
 - key : SpeedRef
 - value : K

기타 지식

  • exif vs tiff
    • exif=8bits / tiff=16bits 차이

Ref.

https://medium.com/@ingakyt/extracting-metadata-from-jpgs-or-image-urls-by-using-cgimageproperties-cgimagesource-nsdata-and-38df6cd900df

https://gist.github.com/rsattar/b06060df7ea293b398d1 https://dev.classmethod.jp/smartphone/iphone/uiimagepickercontroller-exifgps/ https://www.reddit.com/r/iOSProgramming/comments/3asmug/help_with_writing_gps_data_to_image/ http://code.i-harness.com/en/q/9505fa