【问题标题】:How to retrieve Custom values to Image metadata in swift 4?如何在 swift 4 中检索自定义值到图像元数据?
【发布时间】:2018-12-27 08:13:06
【问题描述】:

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

我使用 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)
    }

现在我将在另一个视图控制器中获取存储的图像,如下所述:

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
    }

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

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

【问题讨论】:

  • kCGImagePropertyExifDictionary 不是String。试试这个metadata?[kCGImagePropertyExifDictionary] as? NSMutableDictionary
  • @Kamran - 感谢您的快速回复。我会检查并回复你。
  • @Kamran - 得到相同的崩溃 kamran。在 ImagePickerControllerDelegate 方法中的这一行 let metaStr: NSDictionary = ["Name": self.m_NameTxtFd.text!, "Phone": self.m_PhoneNumberTxtFd.text!, "Price": self.m_PriceTxtFd.text!] exifData?.setValue(metaStr, forKey: kCGImagePropertyExifDictionary as String) crash 是 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
  • 在这一行let metaStr: String = "\(self.m_NameTxtFd.text!),\(self.m_PhoneNumberTxtFd.text!),\(self.m_PriceTxtFd.text!)" 上,您的任何/所有textField 文本都有可能是nil
  • 要设置或获取任何具有Exif 的字典键的值,请使用上述方法。

标签: ios iphone swift uiimagepickercontroller


【解决方案1】:

首先创建 NSMutableDictionary 并将值设置为 NSMutableDictionary,当您将值设置为时,您无需再次将元数据设置为直接分配给 NSMutableDictionary 的元数据。

    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

【讨论】:

  • 你试过这个代码吗?这将再次将他带到他已经修复的同一次崩溃中。你能解释一下你在上面的代码中究竟修复了什么吗?
  • 是的,我试过了,它没有崩溃。您只需将值设置为 NSMutableDictionary 并分配给元数据,而无需从元数据中设置值。
  • @AbhishekJadhav - 感谢您的回复。最后我存储了信息。但无法从 Image 中检索信息。仅供参考:我更新了问题。
  • @Sabs 能否请您向上解答在图像中存储信息的问题。
  • 对不起@AbhishekJadhav 我听不懂你在说什么
【解决方案2】:
fileManager.requestImageData(for: fetchResult.object(at: i) as PHAsset, options: requestOptions, resultHandler: { (imagedata, dataUTI, orientation, info ) in

 if let info = info {

if info.keys.contains(NSString(string: "PHImageFileURLKey")) {

 path = info[NSString(string: "PHImageFileURLKey")] as? NSURL

 size = (imagedata! as NSData).length

self.name = PHAssetResource.assetResources(for:fetchResult.object(at: i)).first?.originalFilename
 self.imageData = imagedata

           }
       }
   })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    相关资源
    最近更新 更多