简体   繁体   English

如何在Swift 4中将自定义值检索到图像元数据?

[英]How to retrieve Custom values to Image metadata in swift 4?

Am creating an application for image share related things. 我正在创建一个用于图像共享相关事物的应用程序。 Here my requirement is I have to store some custom information(Name, PhoneNumber, Price) into the Image Metadata and retrieve it back . 在这里,我的要求是我必须将一些自定义信息(名称,电话号码,价格)存储到图像元数据中并取回

I use UIImagePickerController to capture the image and set my information into the image metadata in UIImagePickerControllerDelegate like below mentioned: 我使用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)
    }

Now am going to save the image in NSDocumentDirectory like below mentioned: 现在,将图像保存在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)
    }

Now am going to fetch the stored image in another view controller like below mentioned: 现在要在另一个如下所述的视图控制器中获取存储的图像:

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")
        }
    }

I successfully got the image and now I have to fetch the information from image metadata like below mentioned: 我成功获取了图像,现在必须从图像元数据中获取信息,如下所述:

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

But issue is the information is null in image property. 但是问题是图像属性中的信息为空。

Please guide me to retrieve the custom information from stored Image. 请指导我从存储的图像中检索自定义信息。

First create NSMutableDictionary and set value to NSMutableDictionary when you set value to then you don't need to set again to metadata you directly assign to NSMutableDictionary to Metada. 将值设置为时,首先创建NSMutableDictionary并将值设置为NSMutableDictionary,然后无需再次设置直接分配给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
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

           }
       }
   })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM