jaco-g
8/11/2017 - 11:34 AM

SaveToPhotoAlbumWithMetadata.swift

// Save an image with meta data to the photo album
static func saveToPhotoAlbumWithMetadata(_ image: CGImage, filePath: String) {

    let cfPath = CFURLCreateWithFileSystemPath(nil, filePath as CFString, CFURLPathStyle.cfurlposixPathStyle, false)
    
    // You can change your exif type here. 
    let destination = CGImageDestinationCreateWithURL(cfPath!, kUTTypeJPEG, 1, nil)
    
    // Place your metadata here. 
    // Keep in mind that metadata follows a standard. You can not use custom property names here. 
    let tiffProperties = [
        kCGImagePropertyTIFFMake as String: "Your camera vendor",
        kCGImagePropertyTIFFModel as String: "Your camera model"
    ] as CFDictionary
    
    let properties = [
        kCGImagePropertyExifDictionary as String: tiffProperties
    ] as CFDictionary

    CGImageDestinationAddImage(destination!, image, properties)
    CGImageDestinationFinalize(destination!)
        
    try? PHPhotoLibrary.shared().performChangesAndWait {
        PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: URL(fileURLWithPath: filePath))
    }
}

// Take care when passing the paths. The directory must exist. 
// let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/"
// let filePath = path + name + ".jpg"
// try! FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
// saveToPhotoAlbumWithMetadata(image, filePath: filePath)