Save The Exif Metadata Using The New PHPhotolibrary

Metadata lost when saving photo using PHPhotoLibrary

I think the method

creationRequestForAssetFromImage:(UIImage *)image;

saves only image data. It doesn't include metadata.

If you want to save a image with metadata, you can do it by the following step.

First Save your image in temporary folder and get its path as NSURL.
Then Call the method

creationRequestForAssetFromImageAtFileURL:(NSURL *)fileURL;

with the NSURL you get in First step.

Modifing metadata from existing phAsset seems not working

Sorry this isn't a full answer but it covers one part of your question. I noticed you are placing the StarRating in the wrong place. You need to place it in a IPTC dictionary. Also the properties data is stored as strings. Given you have the imageProperties you can add the star rating as follows and read it back using the following two functions

func setIPTCStarRating(imageProperties : NSMutableDictionary, rating : Int) {
if let iptc = imageProperties[kCGImagePropertyIPTCDictionary] as? NSMutableDictionary {
iptc[kCGImagePropertyIPTCStarRating] = String(rating)
} else {
let iptc = NSMutableDictionary()
iptc[kCGImagePropertyIPTCStarRating] = String(rating)
imageProperties[kCGImagePropertyIPTCDictionary] = iptc
}
}

func getIPTCStarRating(imageProperties : NSMutableDictionary) -> Int? {
if let iptc = imageProperties[kCGImagePropertyIPTCDictionary] as? NSDictionary {
if let starRating = iptc[kCGImagePropertyIPTCStarRating] as? String {
return Int(starRating)
}
}
return nil
}

As the imageProperties you get from the image are not mutable you need to create a mutable copy of these properties first before you can call the functions above. When you create your image to save use the mutable properties in your call to CGImageDestinationAddImage()

if let mutableProperties = imageProperties.mutableCopy() as? NSMutableDictionary {
setIPTCStarRating(imageProperties:mutableProperties, rating:rating)
}

One other point you are creating an unnecessary UIImage. If you use CGImageDestinationAddImageFromSource() instead of CGImageDestinationAddImage() you can use the imageSource you created earlier instead of loading the image data into a UIImage.

EXIF Data not saving with image?

To Save the detail with Photo using Photos framework, You have to write the image to a temporary file and add the metadata to the temporary file using CGImageDestinationAddImage.

Then you can read the data from the temporary file and save it to the Photos library by using [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL].

Metadata (EXIF, TIFF, etc.) in Photokit

You can achieve the same result by using a temporary file. For example:

import Photos
import ImageIO
import MobileCoreServices

PHPhotoLibrary.sharedPhotoLibrary().performChanges ({
let temporaryPath = ... // a path in the App's documents or cache directory
let fileURL = NSURL.fileURLWithPath (temporaryPath)!

// custom UIImagePNGRepresentation function is implemented below
let PNGdataWithMetadata = UIImagePNGRepresentation (photo, copyright: "Image Copyright",
author: "Image Author", description: "Image Description")
let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImageAtFileURL (fileURL)
}, completionHandler: { (success, error) in
// to-do: delete the temporary file
println ("completion \(success) \(error)")
})

// what the custom UIImagePNGRepresentation function does is create a PNG representation that contains the given metadata. The function assumes all functions return valid data
// to add the metadata to the PNG representation of the image we:
// 1. create the PNG representation and read the default values (these will include things like the image's size)
// 2. add the metadata items (see CGImageProperties Reference for the valid keys)
// 3. create an image destination where the new PNG will be created and assigned the metadata dictionary

public func UIImagePNGRepresentation (image : UIImage, # copyright : String, # author : String, # description : String) -> NSData {
// wrap the valid metadata values in a dictionary
var PNGmetadata = [String : AnyObject] ()
PNGmetadata [kCGImagePropertyPNGCopyright] = copyright
PNGmetadata [kCGImagePropertyPNGAuthor] = author
PNGmetadata [kCGImagePropertyPNGDescription] = description

let metadata = [kCGImagePropertyPNGDictionary as String : PNGmetadata]
return UIImagePNGRepresentation (image, metadata: metadata)
}

public func UIImagePNGRepresentation (image : UIImage, # metadata : [String : [String : AnyObject]]) -> NSData {
let imageSource = CGImageSourceCreateWithData (UIImagePNGRepresentation (image), nil)
let defaultAttributes = CGImageSourceCopyPropertiesAtIndex (imageSource, 0, nil) as NSDictionary
let extendedAttributes = defaultAttributes.mutableCopy () as NSMutableDictionary

for item in metadata {
extendedAttributes [item.0] = item.1
}

var outputPNGdata = NSMutableData ()
let imageDestination = CGImageDestinationCreateWithData (outputPNGdata, kUTTypePNG, 1, nil)
CGImageDestinationAddImageFromSource (imageDestination, imageSource, 0, extendedAttributes)
let success = CGImageDestinationFinalize (imageDestination)
assert (success, "Fail!")

return outputPNGdata
}

I'm copying-pasting code from my project, hopefully I'm not missing something.



Related Topics



Leave a reply



Submit