Skip to content

Instantly share code, notes, and snippets.

@mczachurski
Last active July 15, 2021 19:43
Show Gist options
  • Save mczachurski/601442aaeda831db92a9b102d7bbe334 to your computer and use it in GitHub Desktop.
Save mczachurski/601442aaeda831db92a9b102d7bbe334 to your computer and use it in GitHub Desktop.
import Foundation
import AppKit
import AVFoundation
extension NSImage {
@objc var CGImage: CGImage? {
get {
guard let imageData = self.tiffRepresentation else { return nil }
guard let sourceData = CGImageSourceCreateWithData(imageData as CFData, nil) else { return nil }
return CGImageSourceCreateImageAtIndex(sourceData, 0, nil)
}
}
}
let output = "wallpapers-new/output.heic"
let quality = 0.9
var imageData: Data? = nil
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let destinationData = NSMutableData()
if let destination = CGImageDestinationCreateWithData(destinationData, AVFileType.heic as CFString, 16, nil) {
let options = [kCGImageDestinationLossyCompressionQuality: quality]
for index in 1...16 {
let fileURL = dir.appendingPathComponent("wallpapers-new/\(index).png")
let orginalImage = NSImage(contentsOf: fileURL)
if let cgImage = orginalImage?.CGImage {
if index == 1 {
let imageMetadata = CGImageMetadataCreateMutable()
let imageMetadataTag = CGImageMetadataTagCreate("http://ns.apple.com/namespace/1.0/" as CFString,
"apple_desktop" as CFString,
"solar" as CFString,
CGImageMetadataType.string,
"YnBsaXN0MDDRAQJSc2mvEBADDBAUGBwgJCgsMDQ4PEFF1AQFB" +
"gcICQoLUWlRelFhUW8QACNAcO7vOubr3yO/1e+pmkOtXBAB1A" +
"QFBgcNDg8LEAEjQFRxqCKOFiAjwCR6waUkDgHUBAUGBxESEws" +
"QAiNAVZV4BI4c+CPAEP2uFrMcrdQEBQYHFRYXCxADI0BWtALK" +
"mrjwIz/2ObLnx6l21AQFBgcZGhsLEAQjQFfTrJlEjnwjQByrL" +
"le1Q0rUBAUGBx0eHwsQBSNAWPrrmI0ISCNAKiwhpSRpc9QEBQ" +
"YHISIjCxAGI0BgJff9KDpyI0BENTOsilht1AQFBgclJicLEAc" +
"jQGbHdYIVQKojQEq3fAg86lXUBAUGBykqKwsQCCNAbTGmpC2Y" +
"RiNAQ2WFOZGjntQEBQYHLS4vCxAJI0BwXfII2B+SI0AmLcjfu" +
"C7g1AQFBgcxMjMLEAojQHCnF6YrsxcjQBS9AVBLTq3UBAUGBz" +
"U2NwsQCyNAcTcSnimmjCPAGP5E0ASXJtQEBQYHOTo7CxAMI0B" +
"xgSADjxK2I8AoalieOTyE1AQFBgc9Pj9AEA0jQHNWsnnMcWIj" +
"wEO+oq1pXr8QANQEBQYHQkNEQBAOI0ABZpkFpAcAI8BKYGg/V" +
"vMf1AQFBgdGR0hAEA8jQErBKblRzPgjwEMGElBIUO0ACAALAA" +
"4AIQAqACwALgAwADIANAA9AEYASABRAFMAXABlAG4AcAB5AII" +
"AiwCNAJYAnwCoAKoAswC8AMUAxwDQANkA4gDkAO0A9gD/AQEB" +
"CgETARwBHgEnATABOQE7AUQBTQFWAVgBYQFqAXMBdQF+AYcBk" +
"AGSAZsBpAGtAa8BuAHBAcMBzAHOAdcB4AHpAesB9AAAAAAAAA" +
"IBAAAAAAAAAEkAAAAAAAAAAAAAAAAAAAH9" as CFTypeRef)
let success = CGImageMetadataSetTagWithPath(imageMetadata, nil, "xmp:solar" as CFString, imageMetadataTag!)
if !success {
print("Error!!!")
}
CGImageDestinationAddImageAndMetadata(destination, cgImage, imageMetadata, options as CFDictionary)
} else {
CGImageDestinationAddImage(destination, cgImage, options as CFDictionary)
}
}
}
CGImageDestinationFinalize(destination)
imageData = destinationData as Data
let outputURL = dir.appendingPathComponent(output)
try! imageData?.write(to: outputURL)
}
}
@fuekiin
Copy link

fuekiin commented Jul 8, 2018

Great work! Are you still planing to release an App?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment