简体   繁体   English

使用Swift在iOS上本地保存和检索照片及其所有元数据

[英]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.

相关问题 使用Swift(iOS)和Photos框架保存自定义图像的元数据 - Save metadata for custom image with Swift (iOS) and Photos framework 使用Swift将图像与Exif数据保存到iOS中的照片库 - Save images to photos gallery in iOS with exif data using swift 如何使用Photos Framework在iOS 9中获取图像元数据 - How to get image metadata in iOS 9 using Photos Framework 如何使用Swift在iOS Photos应用程序中选择所有UICollectionView单元格? - How to select ALL UICollectionView cells like in the iOS Photos app using Swift? 当用户尝试使用具有选定照片权限的 PHPickerViewController 在 IOS 14、swift 5 中检索视频时,获取视频数据为零 - Getting video data nil when user tries to retrieve video using PHPickerViewController with selected photos permission in IOS 14, swift 5 如何使用ALAssetsLibrary枚举iOS 8上的所有照片 - How to enumerate all photos on iOS 8 using ALAssetsLibrary iOS Shoutcast 使用 Swift 访问元数据 - iOS Shoutcast access metadata using Swift iOS - 从设备中获取所有照片并将其保存到应用程序 - iOS - Fetch all photos from device and save them to app 使用 Swift 和 iOS 保存文件 - Save file using Swift and iOS iOS Swift,无法使用NSPredicate通过PHFetchOptions中的隐藏照片进行过滤 - IOS Swift, can not filter by hidden photos in PHFetchOptions using NSPredicate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM