2014-10-05 5 views
9

In iOS 8, mit dem neuen PhotoKit, die Art und Weise ein neues Bild mit -[PHPhotoLibrary performChanges:completionHandler], auf diese Weise zu erstellen:Metadaten (EXIF, TIFF, etc.) in PhotoKit

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
    PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; 
    // TODO set metadata? 
} completionHandler:^(BOOL success, NSError *error) { 
    if(success) 
    { 
     // do stuff 
    } 
}]; 

aber ich kann‘ Es gibt keine Dokumentation zum Festlegen von Metadaten für das Bild.

In der alten ALAssetLibrary (die immer noch funktioniert und immer noch der richtige Weg ist), würden Sie einfach -[ALAssetLibrary writePhotoToSavedPhotosAlbum:metadata:completionBlock:] verwenden, damit Sie ein Metadatenwörterbuch übergeben können.

Gibt es eine äquivalente Methode in PhotoKit?

Antwort

8

Sie können dasselbe Ergebnis erzielen, indem Sie eine temporäre Datei verwenden. Zum Beispiel:

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 
} 

Ich bin Kopieren-Einfügen von Code aus meinem Projekt, hoffentlich bin ich dabei nicht etwas.

+0

Sie haben den Code, der tatsächlich speichert die Daten in der Datei URL;) Beachten Sie auch, dass die Kamera App speichert sie als JPGs nicht PNG. Das Speichern als PNG ergab für mich eine Datei von 4,5 MB, während es bei der Kamera nur 850 KB sind. – Joey

+0

Dies wird ein neues Bild erstellen. Wissen Sie, wie Sie die Metadaten eines vorhandenen Fotoassets bearbeiten können? – Ryuk

Verwandte Themen