I’m trying to add a tag/copyright of some kind to UIImages created in my app so that if they are saved to the camera roll/photos library and then later imported (even if the app is deleted & re-installed), it can be detected.
I have tried the following code however I simply continue to get “No copyright information found”.
This code is used for saving the image:
func saveImageWithMetadata(_ image: UIImage, completion: @escaping (Bool) -> Void) {
let metadata: [String: Any] = [
kCGImagePropertyIPTCCopyrightNotice as String: "Copyright Company Name"
]
guard let imageData = image.pngData() else {
completion(false)
return
}
guard let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil) else {
completion(false)
return
}
let newImageData = NSMutableData()
guard let imageDestination = CGImageDestinationCreateWithData(newImageData, kUTTypePNG, 1, nil) else {
completion(false)
return
}
CGImageDestinationAddImageFromSource(imageDestination, imageSource, 0, metadata as CFDictionary)
if !CGImageDestinationFinalize(imageDestination) {
completion(false)
return
}
PHPhotoLibrary.shared().performChanges({
let options = PHAssetResourceCreationOptions()
PHAssetCreationRequest.forAsset().addResource(with: .photo, data: newImageData as Data, options: options)
}) { (success, error) in
completion(success && error == nil)
}
}
And this is the importing code:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
if let imageUrl = info[.imageURL] as? URL {
guard let imageSource = CGImageSourceCreateWithURL(imageUrl as CFURL, nil) else {
print("Unable to create image source.")
return
}
if let metadata = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [String: Any] {
if let iptcMetadata = metadata[kCGImagePropertyIPTCDictionary as String] as? [String: Any],
let copyright = iptcMetadata[kCGImagePropertyIPTCCopyrightNotice as String] as? String {
print("Image has copyright information: (copyright)")
} else {
print("No copyright information found.")
}
}
}
}
I’ve been doing my head in looking around for a straightforward solution to this, and have not been able to find up to date relevant answers to this issue.