ios - 如何在 swift 4 中检索图像元数据的自定义值?

标签 ios iphone swift uiimagepickercontroller

我正在创建一个图像共享相关的应用程序。这里我的要求是我必须将一些自定义信息(姓名、电话号码、价格)存储到图像元数据中并将其取回

我使用 UIImagePickerController 来捕获图像并将我的信息设置到 UIImagePickerControllerDelegate 中的图像元数据中,如下所述:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    picker.dismiss(animated: true, completion: nil)
    let profileImage = info[UIImagePickerControllerOriginalImage] as? UIImage
    let imageData: Data = UIImageJPEGRepresentation(profileImage!, 1)!
    let cgImgSource: CGImageSource = CGImageSourceCreateWithData(imageData as CFData, nil)!
    let uti: CFString = CGImageSourceGetType(cgImgSource)!
    let dataWithExif: NSMutableData = NSMutableData(data: imageData)
    let destination: CGImageDestination = CGImageDestinationCreateWithData((dataWithExif as CFMutableData), uti, 1, nil)!
    let imageProoperties = CGImageSourceCopyPropertiesAtIndex(cgImgSource, 0, nil)! as NSDictionary
    let mutable: NSMutableDictionary = imageProoperties.mutableCopy() as! NSMutableDictionary

    let EXIFDictionary: NSMutableDictionary = (mutable[kCGImagePropertyExifDictionary as String] as? NSMutableDictionary)!
    print("Before Modification: \(EXIFDictionary)")
    EXIFDictionary[kCGImagePropertyExifUserComment as String] = "\(self.m_NameTxtFd.text!):\(self.m_PhoneNumberTxtFd.text!):\(self.m_PriceTxtFd.text!)"
    mutable[kCGImagePropertyExifDictionary as String] = EXIFDictionary

    CGImageDestinationAddImageFromSource(destination, cgImgSource, 0, (mutable as CFDictionary))
    CGImageDestinationFinalize(destination)

    let testImage: CIImage = CIImage(data: dataWithExif as Data, options: nil)!
    let newProperties: NSDictionary = testImage.properties as NSDictionary
    print("After Modification : \(newProperties)") //Here I Got My Information is Stored Successfully

    self.m_ImgView.image = self.convert(cmage: testImage)
    self.saveImageDocumentDirectory()
}

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }

现在我要像下面提到的那样将图像保存在 NSDocumentDirectory 中:

func saveImageDocumentDirectory(){
        let fileManager = FileManager.default
        let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("apple.jpg")
        let image = self.m_ImgView.image
        print(paths)
        let imageData = UIImageJPEGRepresentation(image!, 0.5)
        fileManager.createFile(atPath: paths as String, contents: imageData, attributes: nil)
    }

现在我要在另一个 View Controller 中获取存储的图像,如下所示:

func getImage(){
        let fileManager = FileManager.default
        let imagePAth = (self.getDirectoryPath() as NSString).appendingPathComponent("apple.jpg")
        print(imagePAth)
        if fileManager.fileExists(atPath: imagePAth){
            self.m_ImgView.image = UIImage(contentsOfFile: imagePAth)
            self.fetchImageDetails()
        }else{
            print("No Image")
        }
    }

我成功地得到了图像,现在我必须像下面提到的那样从图像元数据中获取信息:

func fetchImageDetails() {
        let profileImage = self.m_ImgView.image!
        let ciImage: CIImage = CIImage(cgImage: profileImage.cgImage!)
        let newProperties: NSDictionary = ciImage.properties as NSDictionary
    }

但问题是图像属性中的信息为空。

请指导我从存储的图像中检索自定义信息。

最佳答案

首先创建 NSMutableDictionary 并设置 value 到 NSMutableDictionary 当你设置 value 时你不需要再次设置你直接分配给 NSMutableDictionary 到 Metada 的元数据。

    let metadata = info[UIImagePickerControllerMediaMetadata] as? NSMutableDictionary                
    let exifData = NSMutableDictionary()            
    let metaStr = "\(self.m_NameTxtFd.text!),\(self.m_PhoneNumberTxtFd.text!),\(self.m_PriceTxtFd.text!)"
    exifData.setValue(metaStr, forKey: kCGImagePropertyExifDictionary as String)                       
    metadata = exifData

关于ios - 如何在 swift 4 中检索图像元数据的自定义值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51418427/

相关文章:

ios - 为什么 XMPP 函数什么都不返回?

ios - Swift 中的核心数据通知

iphone - Lua脚本推送类函数PN.click()

swift - 您如何以简洁易读的方式顺序链接可观察对象

iphone - 你如何处理 iOS 中的 Web 服务?核心数据呢?

iphone - 核心数据关系列表

arrays - 快速搜索数组中的字符串并返回索引

objective-c - 有没有一种方法可以检查哪个文件已加载到UIImage对象中?

ios - 带有 UIBarButtonItems 的 UIToolBar 等间距,与标题长度无关

ios - 获取数组的值作为整数并实时显示在按钮上