How to enrich UIImage data with metadata - ios

In my app I'm trying to have image data have metadata (location, timestamp etc.). I'm using UIImagePickerController to do image capture, and the delegate function of which has:
info[UIImagePickerController.InfoKey.originalImage]
info[UIImagePickerController.InfoKey.phAsset]
info[UIImagePickerController.InfoKey.mediaMetadata]
So for the images picked from the library .phAsset has everything I need. I just use .getDataFromPHAsset function to get the enriched data. However, for the images that were just taken .phAsset is nil. I thought about trying to somehow combine .originalImage and .mediaMetadata together into single Data object, but can't get the result desired. I tried to use this approach: https://gist.github.com/kwylez/a4b6ec261e52970e1fa5dd4ccfe8898f
I know I can also make custom imageCapture controller, using AVCaptureSession, and use AVCapturePhoto function .fileDataRepresentation() on didFinishProcessingPhoto delegate call, but that's not my first choice.
Any kind of help is highly appreciated.

I'm pretty sure mediaMetadata will not have location info. Use this CLLocation extension.
Then use the function below to add the metadata to the image data:
func addImageProperties(imageData: Data, properties: NSMutableDictionary) -> Data? {
let dict = NSMutableDictionary()
dict[(kCGImagePropertyGPSDictionary as String)] = properties
if let source = CGImageSourceCreateWithData(imageData as CFData, nil) {
if let uti = CGImageSourceGetType(source) {
let destinationData = NSMutableData()
if let destination = CGImageDestinationCreateWithData(destinationData, uti, 1, nil) {
CGImageDestinationAddImageFromSource(destination, source, 0, dict as CFDictionary)
if CGImageDestinationFinalize(destination) == false {
return nil
}
return destinationData as Data
}
}
}
return nil
}
Usage:
if let imageData = image.jpegData(compressionQuality: 1.0), let metadata = locationManager.location?.exifMetadata() {
if let newImageData = addImageProperties(imageData: imageData, properties: metadata) {
// newImageData now contains exif metadata
}
}

Related

Saving Data as UIImage while preserving the original image characteristics

I am removing exif and location metadata from images using Photos and image I/O frameworks:
First I get Data from PHAssets:
let manager = PHImageManager()
manager.requestImageData(for: currentAsset, options: options) { (data, dataUTI, orientation, info) in
if let data = data {
dataArray.append(data)
}
}
Then I use this function to remove metadata:
fileprivate func removeMetadataFromData(data: Data) -> NSMutableData? {
guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {return nil}
guard let type = CGImageSourceGetType(source) else {return nil}
let count = CGImageSourceGetCount(source)
let mutableData = NSMutableData(data: data)
guard let destination = CGImageDestinationCreateWithData(mutableData, type, count, nil) else {return nil}
let removeExifProperties: CFDictionary = [String(kCGImagePropertyExifDictionary) : kCFNull, String(kCGImagePropertyGPSDictionary): kCFNull] as CFDictionary
for i in 0..<count {
CGImageDestinationAddImageFromSource(destination, source, i, removeExifProperties)
}
guard CGImageDestinationFinalize(destination) else {return nil}
return mutableData
}
Then I use this to create UIImage from NSMutableData objects that I get from previous function:
let image = UIImage(data: mutableData as Data)
and I save the image to user's library like so:
PHPhotoLibrary.shared().performChanges({
let request = PHAssetChangeRequest.creationRequestForAsset(from: image)
let placeholder = request.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: collection)
if let placeholder = placeholder, let albumChangeRequest = albumChangeRequest {
albumChangeRequest.addAssets([placeholder] as NSArray)
}
return mutableData
}
The problem I have is that using this method, the output file is compressed, and also the name and DPI of the resulting image is different from the original image. I want to keep everything the same as the original image and just remove the metadata. Is there a way to do that?
The problem is the round-trip through UIImage. Just save the Data obtained from requestImageDataAndOrientation.
func saveCopyWithoutLocation(for asset: PHAsset) {
let options = PHImageRequestOptions()
manager.requestImageDataAndOrientation(for: asset, options: options) { data, dataUTI, orientation, info in
guard let data = data else { return }
self.library.performChanges {
let request = PHAssetCreationRequest.forAsset()
request.addResource(with: .photo, data: data, options: nil)
request.location = nil
} completionHandler: { success, error in
if success {
print("successful")
} else {
print(error?.localizedDescription ?? "no error?")
}
}
}
}
Now, that only removes location. If you really want to remove more EXIF data obtained through CGImageSourceCreateWithData, you can do that. But just avoid an unnecessary round-trip through a UIImage. It is the whole purpose to use CGImageSource functions, namely that you can change metadata without changing the underlying image payload. (Round-tripping through UIImage is another way to strip meta data, but as you have discovered, it changes the image payload, too, though often not observable to the naked eye.)
So, if you want, just take the data from CGImageDestination functions directly, and pass that to PHAssetCreationRequest. But I might advise being a little more discriminating about which EXIF metadata you choose to remove, because some of it is important, non-confidential image data (e.g., likely the DPI is in there).
Regarding the filename, I'm not entirely sure you can control that. E.g., I've had images using the above location-stripping routine, and some preserve the file name in the copy, and others do not (and the logic of which applies is not immediately obvious to me; could be the sourceType). Obviously, you can use PHAssetChangeRequest rather than PHAssetCreationRequest, and you can just update the original PHAsset, and that would preserve the file name, but you might not have intended to edit the original asset and may have preferred to make a new copy.

cannot assign value of type 'UIImage?' to type 'NSData?' in swift 3

I am using CoreData for an app. I have set image as BinaryData in data model. But I have fetched image from server as UIImage and it throws error as:
cannot assign value of type 'UIImage?' to type 'NSData?
I searched but couldn't find any resemblance solution for it. Can anyone help me in swift 3?
My code is:
let url1:URL = URL(string:self.appDictionary.value(forKey: "image") as! String)!
let picture = "http://54.243.11.100/storage/images/news/f/"
let strInterval = String(format:"%#%#",picture as CVarArg,url1 as CVarArg) as String as String
let url = URL(string: strInterval as String)
SDWebImageManager.shared().downloadImage(with: url, options: [],progress: nil, completed: {[weak self] (image, error, cached, finished, url) in
if self != nil {
task.imagenews = image //Error:cannot assign value of type 'UIImage?' to type 'NSData?'
}
})
The error message is pretty clear - you cannot assign UIImage object to a variable of NSData type.
To convert UIImage to Swift's Data type, use UIImagePNGRepresentation
var data : Data = UIImagePNGRepresentation(image)
Note that if you're using Swift, you should be using Swift's type Data instead of NSData
You must convert, image into Data (or NSData) to support imagenews data type.
Try this
let url1:URL = URL(string:self.appDictionary.value(forKey: "image") as! String)!
let picture = "http://54.243.11.100/storage/images/news/f/"
let strInterval = String(format:"%#%#",picture as CVarArg,url1 as CVarArg) as String as String
let url = URL(string: strInterval as String)
SDWebImageManager.shared().downloadImage(with: url, options: [],progress: nil, completed: {[weak self] (image, error, cached, finished, url) in
if self != nil {
if let data = img.pngRepresentationData { // If image type is PNG
task.imagenews = data
} else if let data = img.jpegRepresentationData { // If image type is JPG/JPEG
task.imagenews = data
}
}
})
// UIImage extension, helps to convert Image into data
extension UIImage {
var pngRepresentationData: Data? {
return UIImagePNGRepresentation(img)
}
var jpegRepresentationData: Data? {
return UIImageJPEGRepresentation(self, 1.0)
}
}

How do you set kCGImagePropertyExifUserComment on a CGImageMetadataRef?

How do you set kCGImagePropertyExifUserComment on a CGImageMetadataRef in iOS? These are the only functions I have found and they are only for MacOS
https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.9.sdk/System/Library/Frameworks/ImageIO.framework/Versions/A/Headers/CGImageMetadata.h
Given image data you can get it's properties with the following
func getImageDataProperties(_ data: Data) -> NSDictionary? {
if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil)
{
if let dictionary = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) {
return dictionary
}
}
return nil
}
The exif comments can be accessed as follows with the properties
if let properties = getImageDataProperties(data) {
if let exif = properties[kCGImagePropertyExifDictionary] as? NSDictionary {
if let comment = exif[kCGImagePropertyExifUserComment] as? String {
...
}
}
}
Once you have edited the comments you can save your image with the following given you original image data and your altered properties dictionary
// add image properties (exif, gps etc) to image
func addImageProperties(imageData: Data, properties: NSMutableDictionary) -> Data? {
// create an imagesourceref
if let source = CGImageSourceCreateWithData(imageData as CFData, nil) {
// this is of type image
if let uti = CGImageSourceGetType(source) {
// create a new data object and write the new image into it
let destinationData = NSMutableData()
if let destination = CGImageDestinationCreateWithData(destinationData, uti, 1, nil) {
// add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
CGImageDestinationAddImageFromSource(destination, source, 0, properties)
if CGImageDestinationFinalize(destination) == false {
return nil
}
return destinationData as Data
}
}
}
return nil
}
Create a mutable copy of the CGImageMetadataRef with CGImageMetadataCreateMutableCopy. Then set the property with
CGImageMetadataSetValueMatchingImageProperty(
mutableCopy,
kCGImagePropertyExifDictionary,
kCGImagePropertyExifUserComment,
value)
where value is your comment as a CFTypeRef.

How do I create a UIImage with the DPI set?

I have a UIImage, and I wish to save it to Photos with the DPI metadata set. I understand that a UIImage is immutable, so I have to create a new UIImage. I've used this answer as a reference to create an swift extension function on UIImage that produces a new UIImage with the DPI set. I'm successfully saving this to Photos with UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil). I email this to myself (set as 'Actual Size') and open it in Preview but the DPI is always left at 72.
Here is the function that I converted:
func imageWithDPI(dpi :Int) -> UIImage? {
guard let sourceImageData = UIImageJPEGRepresentation(self, 0.8) else {
print("Couldn't make PNG Respresentation of UIImage")
return nil
}
guard let source = CGImageSourceCreateWithData(sourceImageData, nil) else {
print("Couldn't create source with sourceImageData")
return nil
}
var metadata = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [String:AnyObject] ?? [String:AnyObject]()
metadata[kCGImagePropertyDPIWidth as String] = dpi
metadata[kCGImagePropertyDPIHeight as String] = dpi
var exifDictionary = metadata[kCGImagePropertyTIFFDictionary as String] as? [String:AnyObject] ?? [String:AnyObject]()
exifDictionary[kCGImagePropertyTIFFXResolution as String] = dpi
exifDictionary[kCGImagePropertyTIFFYResolution as String] = dpi
metadata[kCGImagePropertyTIFFDictionary as String] = exifDictionary
var jfifDictionary = metadata[kCGImagePropertyJFIFDictionary as String] as? [String:AnyObject] ?? [String:AnyObject]()
jfifDictionary[kCGImagePropertyJFIFXDensity as String] = dpi
jfifDictionary[kCGImagePropertyJFIFYDensity as String] = dpi
jfifDictionary[kCGImagePropertyJFIFVersion as String] = 1
metadata[kCGImagePropertyJFIFDictionary as String] = jfifDictionary
guard let uti = CGImageSourceGetType(source) else {
print("Couldn't get type from source")
return nil
}
let destinationImageData = NSMutableData()
guard let destination = CGImageDestinationCreateWithData(destinationImageData, uti, 1, nil) else {
print("Couldn't create destination with destinationImageData")
return nil
}
CGImageDestinationAddImageFromSource(destination, source,0, metadata)
CGImageDestinationFinalize(destination)
return UIImage(data: destinationImageData)
}
I'm kind of out of my depth in Core Graphics, so any advice would be much appreciated.
Many thanks in advance.
UIImage may be stripping all of the metadata from the image when you create it from the destinationImageData, including the DPI. I recently ran into this issue when sharing images via iOS’s UIActivityItemProvider API. If I returned a UIImage * from the - (id)item method then all metadata was lost. The fix was to return either an NSData * or an NSURL *.
Try returning the destinationImageData directly, or saving it to a file & emailing that to yourself.
I’m curious as to why you want to set the DPI for an image? DPI is mostly meaningless for nearly all digital images these days (unless an image is meant to exactly represent the dimensions of a physical object). Actual pixel resolution is far more important than DPI.

Swift: How to Delete EXIF data from picture taken with AVFoundation?

I'm trying to get rid of the EXIF data from a picture taken with AVFoundation, How can I do this in swift (2) preferred, Objective-C is okay too, I know how to convert the code to swift.
Why?
I have done my research and I see a lot of famous Social Media (Reddit Source and many more) do remove EXIF data for identity purposes and other purposes.
If you think this is duplicate post, please read what I'm asking and provide link. Thank you.
My answer is based a lot on this previous question. I adapted the code to work on Swift 2.0.
class ImageHelper {
static func removeExifData(data: NSData) -> NSData? {
guard let source = CGImageSourceCreateWithData(data, nil) else {
return nil
}
guard let type = CGImageSourceGetType(source) else {
return nil
}
let count = CGImageSourceGetCount(source)
let mutableData = NSMutableData(data: data)
guard let destination = CGImageDestinationCreateWithData(mutableData, type, count, nil) else {
return nil
}
// Check the keys for what you need to remove
// As per documentation, if you need a key removed, assign it kCFNull
let removeExifProperties: CFDictionary = [String(kCGImagePropertyExifDictionary) : kCFNull, String(kCGImagePropertyOrientation): kCFNull]
for i in 0..<count {
CGImageDestinationAddImageFromSource(destination, source, i, removeExifProperties)
}
guard CGImageDestinationFinalize(destination) else {
return nil
}
return mutableData;
}
}
Then you can simply do something like this:
let imageData = ImageHelper.removeExifData(UIImagePNGRepresentation(image))
In my example, I remove the rotation and the EXIF data. You can easily search the keys if you need anything else removed. Just make extra checks on the data generated as it is an optional.
Here is a solution that removes the exif from raw data. Exif in jpeg is inside APP1 frame. Frame start is indicated with FF_E1. Frame end at next FF byte that does not follow 00 value.
var data: Data = ... // read jpeg one way or another
var app1_start = 0
var app1_end = Int.max
for i in 0 ..< data.count {
if data[i] == 0xFF {
if data[i + 1] == 0xE1 {
print("found start \(i)")
app1_start = i
} else if app1_start > 0, data [i] != 0x00 {
app1_end = i - 1
print("found end \(i-1)")
break
}
}
}
data.removeSubrange(Range(app1_start...app1_end))
Data in this example is assumed to be jpeg. Code loops through byte array and stores APP1 start and end. Then removes the data from original mutable data. More about jpeg structure here https://en.wikipedia.org/wiki/JPEG
You have UIImage right?
Then you can convert UIImage to Data and save it to image again new image will not have any EXIF data
Swift 3
let imageData:Data = UIImagePNGRepresentation(image!)!
func saveToPhotoLibrary_iOS9(data:NSData, completionHandler: #escaping (PHAsset?)->()) {
var assetIdentifier: String?
PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) in
if(status == PHAuthorizationStatus.authorized){
PHPhotoLibrary.shared().performChanges({
let creationRequest = PHAssetCreationRequest.forAsset()
let placeholder = creationRequest.placeholderForCreatedAsset
creationRequest.addResource(with: PHAssetResourceType.photo, data: data as Data, options: nil)
assetIdentifier = placeholder?.localIdentifier
}, completionHandler: { (success, error) in
if let error = error {
print("There was an error saving to the photo library: \(error)")
}
var asset: PHAsset? = nil
if let assetIdentifier = assetIdentifier{
asset = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: nil).firstObject//fetchAssetsWithLocalIdentifiers([assetIdentifier], options: nil).firstObject as? PHAsset
}
completionHandler(asset)
})
}else {
print("Need authorisation to write to the photo library")
completionHandler(nil)
}
}
}
Swift 5 version of the accepted answer:
extension Data {
func byRemovingEXIF() -> Data? {
guard let source = CGImageSourceCreateWithData(self as NSData, nil),
let type = CGImageSourceGetType(source) else
{
return nil
}
let count = CGImageSourceGetCount(source)
let mutableData = NSMutableData()
guard let destination = CGImageDestinationCreateWithData(mutableData, type, count, nil) else {
return nil
}
let exifToRemove: CFDictionary = [
kCGImagePropertyExifDictionary: kCFNull,
kCGImagePropertyGPSDictionary: kCFNull,
] as CFDictionary
for index in 0 ..< count {
CGImageDestinationAddImageFromSource(destination, source, index, exifToRemove)
if !CGImageDestinationFinalize(destination) {
print("Failed to finalize")
}
}
return mutableData as Data
}
}

Resources