【发布时间】: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