2014-10-05 7 views
9

में (EXIF, TIFF, आदि) iOS 8 में, नए PhotoKit साथ, जिस तरह से एक नई छवि बनाने के लिए -[PHPhotoLibrary performChanges:completionHandler] के साथ है, इस तरह से:मेटाडाटा Photokit

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

हालांकि, मैं यह कर सकते हैं ' छवि पर मेटाडेटा सेट करने के तरीके पर कोई दस्तावेज नहीं मिला।

पुरानी अलास्सेट लाइब्रेरी (जो अभी भी काम करता है, और अभी भी 'सही' रास्ता आगे बढ़ रहा है) में, आप केवल -[ALAssetLibrary writePhotoToSavedPhotosAlbum:metadata:completionBlock:] का उपयोग करेंगे, जो आपको मेटाडेटा शब्दकोश में पास करने की अनुमति देता है।

क्या फोटोकिट में समकक्ष विधि है?

उत्तर

8

आप एक अस्थायी फ़ाइल का उपयोग करके एक ही परिणाम प्राप्त कर सकते हैं। उदाहरण के लिए:

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 
} 

मैं कॉपी करने-चिपकाने कर रहा हूँ अपने प्रोजेक्ट से कोड, उम्मीद है कि मैं कुछ कमी नहीं कर रहा हूँ।

+0

आपने कोड को छोड़ दिया है जो वास्तव में फ़ाइलURL को डेटा सहेजता है;) यह भी ध्यान रखें कि कैमरा ऐप उन्हें पीपीजी नहीं जेपीजी के रूप में सहेजता है। पीएनजी के रूप में सहेजने के परिणामस्वरूप मेरे लिए 4.5 एमबी फाइल हुई जबकि कैमरा के साथ लिया गया यह सिर्फ 850 केबी है। – Joey

+0

यह नई छवि बनाएगा। क्या आप जानते हैं कि मौजूदा फोटो एसेट्स छवि के मेटाडेटा को कैसे संपादित करें? – Ryuk

संबंधित मुद्दे