斯威夫特 5:
我花了一周的时间来确定所有的部分,将所有部分收集到工作代码中。
这将保存一个UIImage
带有 GPS 元数据的 JPEG 临时文件:
let image:UIImage = mImageView.image! // your UIImage
// create filename
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy.MM.dd-HH.mm.ss"
let now = Date()
let date_time = dateFormatter.string(from: now)
let fileName:String = "your_image_"+date_time+".jpg" // name your file the way you want
let temporaryFolder:URL = FileManager.default.temporaryDirectory
let temporaryFileURL:URL = temporaryFolder.appendingPathComponent(fileName)
// save the image to chosen path
let jpeg = image.jpegData(compressionQuality: 0.85)! // set JPG quality here (1.0 is best)
let src = CGImageSourceCreateWithData(jpeg as CFData, nil)!
let uti = CGImageSourceGetType(src)!
let cfPath = CFURLCreateWithFileSystemPath(nil, temporaryFileURL.path as CFString, CFURLPathStyle.cfurlposixPathStyle, false)
let dest = CGImageDestinationCreateWithURL(cfPath!, uti, 1, nil)
// create GPS metadata from current location
let gpsMeta = gCurrentLocation?.exifMetadata() // gCurrentLocation is your CLLocation (exifMetadata is an extension)
let tiffProperties = [
kCGImagePropertyTIFFMake as String: "Camera vendor",
kCGImagePropertyTIFFModel as String: "Camera model"
// --(insert other properties here if required)--
] as CFDictionary
let properties = [
kCGImagePropertyTIFFDictionary as String: tiffProperties,
kCGImagePropertyGPSDictionary: gpsMeta as Any
// --(insert other dictionaries here if required)--
] as CFDictionary
CGImageDestinationAddImageFromSource(dest!, src, 0, properties)
if (CGImageDestinationFinalize(dest!)) {
print("Saved image with metadata!")
} else {
print("Error saving image with metadata")
}
这是 GPS 元数据扩展(来自https://gist.github.com/chefren/8b50652d67c397a825619f83c8dba6d3):
import Foundation
import CoreLocation
extension CLLocation {
func exifMetadata(heading:CLHeading? = nil) -> NSMutableDictionary {
let GPSMetadata = NSMutableDictionary()
let altitudeRef = Int(self.altitude < 0.0 ? 1 : 0)
let latitudeRef = self.coordinate.latitude < 0.0 ? "S" : "N"
let longitudeRef = self.coordinate.longitude < 0.0 ? "W" : "E"
// GPS metadata
GPSMetadata[(kCGImagePropertyGPSLatitude as String)] = abs(self.coordinate.latitude)
GPSMetadata[(kCGImagePropertyGPSLongitude as String)] = abs(self.coordinate.longitude)
GPSMetadata[(kCGImagePropertyGPSLatitudeRef as String)] = latitudeRef
GPSMetadata[(kCGImagePropertyGPSLongitudeRef as String)] = longitudeRef
GPSMetadata[(kCGImagePropertyGPSAltitude as String)] = Int(abs(self.altitude))
GPSMetadata[(kCGImagePropertyGPSAltitudeRef as String)] = altitudeRef
GPSMetadata[(kCGImagePropertyGPSTimeStamp as String)] = self.timestamp.isoTime()
GPSMetadata[(kCGImagePropertyGPSDateStamp as String)] = self.timestamp.isoDate()
GPSMetadata[(kCGImagePropertyGPSVersion as String)] = "2.2.0.0"
if let heading = heading {
GPSMetadata[(kCGImagePropertyGPSImgDirection as String)] = heading.trueHeading
GPSMetadata[(kCGImagePropertyGPSImgDirectionRef as String)] = "T"
}
return GPSMetadata
}
}
extension Date {
func isoDate() -> String {
let f = DateFormatter()
f.timeZone = TimeZone(abbreviation: "UTC")
f.dateFormat = "yyyy:MM:dd"
return f.string(from: self)
}
func isoTime() -> String {
let f = DateFormatter()
f.timeZone = TimeZone(abbreviation: "UTC")
f.dateFormat = "HH:mm:ss.SSSSSS"
return f.string(from: self)
}
}
就是这样!
您现在可以使用activityViewController
将临时图像(使用temporaryFileURL
)保存到相册,或将其保存为文件,或将其共享到其他应用程序,或任何您想要的。