Skip to content

Instantly share code, notes, and snippets.

@Reflejo
Created January 15, 2016 04:18
Show Gist options
  • Save Reflejo/8e74290d4dc398bebe0b to your computer and use it in GitHub Desktop.
Save Reflejo/8e74290d4dc398bebe0b to your computer and use it in GitHub Desktop.
import Foundation
let pixels: [[UInt8]] = [
[
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00,
0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0x00,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
],
]
let URL = NSURL(fileURLWithPath: "test.gif")
let destination = CGImageDestinationCreateWithURL(URL, kUTTypeGIF, 2, nil)!
for frame in 0 ..< 2 {
var rgbData = pixels[frame].flatMap { [$0, $0, $0] }
let data = CGDataProviderCreateWithCFData(NSData(bytes: &rgbData, length: rgbData.count))
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.None.rawValue)
let imageReference = CGImageCreate(8, 8, 8, 8 * 3, 8 * 3,
CGColorSpaceCreateDeviceRGB(), bitmapInfo, data, nil, false, .RenderingIntentDefault)
if let imageReference = imageReference {
CGImageDestinationAddImage(destination, imageReference, nil)
}
}
CGImageDestinationFinalize(destination)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment