[英]Save and retrieve Photos with all their metadata locally on iOS using swift
I am currently taking photos from the users iPhone making them jpegs, then saving them using file Manager and then saving their paths with core data. 我目前正在从用户iPhone拍摄照片,将其制作为jpeg,然后使用文件管理器进行保存,然后将其路径与核心数据一起保存。
The way I do it I can't show live images or later export the image with its previous meta data. 我这样做的方式无法显示实时图像,也无法稍后使用其先前的元数据导出图像。
My question is what is the correct way to save an image locally with all its meta data and live status so it can be displayed and exported later but only inside the app. 我的问题是将图像及其所有元数据和实时状态保存在本地的正确方法是什么,以便以后可以显示和导出图像,但只能在应用程序内部进行保存。 I don't want the images to be viable outside the application. 我不希望这些图像在应用程序外部可行。
Taking The image: 拍摄图像:
Take a look at the UIImagePickerController
class and use it to take pictures. 看一下UIImagePickerController
类,并用它来拍照。 Taking pictures with this, will NOT save the images automatically to the iOS gallery. 以此拍照,不会自动将图像保存到iOS画廊。
The mentioned UIImagePickerController
will notify a delegate UIImagePickerControllerDelegate
and call the function func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any])
. 提到的UIImagePickerController
将通知委托UIImagePickerControllerDelegate
并调用函数func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any])
。
From the second parameter, the info dictionary, you can request most of the images metadata, livePhoto and so on. 通过第二个参数信息字典,您可以请求大多数图像元数据,livePhoto等。 The necessary keys can be found inside the extension of UIImagePickerController
called UIImagePickerController.InfoKey
. 可以在UIImagePickerController
扩展名为UIImagePickerController.InfoKey
的扩展中找到必需的键。
For example, here is how I then retrieve the images coordinate if available: 例如,如果可以的话,下面是我如何检索图像坐标的方法:
if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
if let asset = result.firstObject, let location = asset.location {
let lat = location.coordinate.latitude
let lon = location.coordinate.longitude
print("Here's the lat and lon \(lat) + \(lon)")
}
}
Saving the image onto the phone within your app?: 将图片保存到应用内的手机上?:
I use JSONEncoder
. 我使用JSONEncoder
。 You can use this class to encode and then decode your custom image class again. 您可以使用此类来编码,然后再次解码自定义图像类。
Your custom image class, which has the UIImage and all the other properties must then implement the Codable
protocol. 然后,具有UIImage和所有其他属性的自定义图像类必须实现Codable
协议。 Afterwards, use the JSONEncoder
to create Data
of the object via its encode()
method and save it to an app private location with the help of a FileManager
. 然后,使用JSONEncoder
通过对象的encode()
方法创建对象的Data
,并在FileManager
的帮助下将其保存到应用程序的私有位置。 Later on, you may read all files from that location again with the FileManager
, decode()
with the JSONEncoder
and voila you have all your images again in the form of your custom image class. 稍后,您可以使用FileManager
再次从该位置读取所有文件,并使用JSONEncoder
decode()
,然后以自定义图像类的形式再次获得所有图像。
Saving the image to the users gallery "Exporting": 将图像保存到用户库“导出”:
Here's an example of how I again save that image to the users gallery as a way of exporting it: 这是一个示例,说明了我如何再次将该图像保存到用户库作为导出图像的方法:
private static func saveImageToGallery(picture: UIImage, lat: Double?, lon: Double?) {
PHPhotoLibrary.shared().performChanges({
let request = PHAssetCreationRequest.creationRequestForAsset(from: picture)
if let _lat = lat, let _lon = lon {
request.location = CLLocation(latitude: _lat, longitude: _lon)
}
})
}
I hope this will be enough to guide you to the correct way. 我希望这足以指导您正确的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.