Created
June 25, 2015 01:27
-
-
Save traviskirton/06319d420426cb0f1cb3 to your computer and use it in GitHub Desktop.
C4Image create with CGImageRef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import C4UI | |
import C4Core | |
import C4Animation | |
public struct PixelData { | |
var a:UInt8 | |
var r:UInt8 | |
var g:UInt8 | |
var b:UInt8 | |
public init() { | |
a = 255 | |
r = 0 | |
g = 0 | |
b = 0 | |
} | |
} | |
class ViewController: C4CanvasController { | |
override func setup() { | |
var pixels = [PixelData]() | |
for x in 0..<256 { | |
for y in 0..<256 { | |
var pixel = PixelData() | |
pixel.r = UInt8(x) | |
pixel.b = UInt8(y) | |
pixels.append(pixel) | |
} | |
} | |
let rgbColorSpace = CGColorSpaceCreateDeviceRGB() | |
let bitmapInfo:CGBitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.rawValue) | |
let bitsPerComponent:Int = 8 | |
let bitsPerPixel:Int = 32 | |
let width : Int = 256 | |
let height : Int = 256 | |
assert(pixels.count == Int(width * height)) | |
var data = pixels // Copy to mutable [] | |
let providerRef = CGDataProviderCreateWithCFData( | |
NSData(bytes: &data, length: data.count * sizeof(PixelData)) | |
) | |
let cgim = CGImageCreate( | |
width, | |
height, | |
bitsPerComponent, | |
bitsPerPixel, | |
width * Int(sizeof(PixelData)), | |
rgbColorSpace, | |
bitmapInfo, | |
providerRef, | |
nil, | |
true, | |
kCGRenderingIntentDefault | |
) | |
let img = C4Image(cgimage: cgim) | |
canvas.add(img) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment