2014-10-05 6 views
9

yılında Meta veri (EXIF, TIFF, vs.),:, yeni bir resim oluşturmak için bir yol bu şekilde, <code>-[PHPhotoLibrary performChanges:completionHandler]</code> ile yeni PhotoKit ile iOS 8'de photokit

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

Ancak, ben can' görüntüde meta verilerin nasıl ayarlanacağına dair bir belge bulun.

Eski ALAssetLibrary'da (hala çalışır ve hala ileriye doğru 'doğru' bir yol olabilir), yalnızca bir meta veri sözlüğünde geçiş yapmanıza olanak veren -[ALAssetLibrary writePhotoToSavedPhotosAlbum:metadata:completionBlock:] kullanırsınız.

PhotoKit'te eşdeğer bir yöntem var mı?

cevap

8

Geçici bir dosya kullanarak aynı sonucu elde edebilirsiniz. Örneğin:

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 
} 

Umarım bir şey eksik değilim, benim projeden kod-yapıştırma taklit ediyorum.

+0

Verileri gerçekte dosyaya kaydeden kodu çıkarmadınız.) Ayrıca, Kamera uygulamasının bunları PNG değil JPG olarak kaydettiğini unutmayın. Fotoğraf makinesi ile alındığında sadece 850 KB iken, PNG olarak kaydedildiğim için 4,5 MB bir dosya sonuçlandı. – Joey

+0

Bu, yeni bir görüntü oluşturur. Varlık fotoğrafının meta verilerini nasıl düzenleyeceğinizi biliyor musunuz? – Ryuk

İlgili konular