Quantcast
Channel: User kubi - Stack Overflow
Viewing all articles
Browse latest Browse all 39

Convert a bit array to a struct array in Swift

$
0
0

Here's what I've got currently. I'm wondering if there's a more elegant way to do it or, better yet, a way to cast my array to the required type without iterating over the entire structure.

My input is a CFDataRef containing pixel data. I'd like to convert it to an array of Pixels.

struct Pixel {    let r: UInt8    let g: UInt8    let b: UInt8    let a: UInt8}var intArray = [UInt8](count: CFDataGetLength(data), repeatedValue: 0)let dataRange = CFRangeMake(0, CFDataGetLength(data))CFDataGetBytes(data, dataRange, &intArray)var pixelArray : [Pixel] = []for var i = 0; i < intArray.count; i += 4 {    let p = Pixel(r: intArray[i], g: intArray[i+1], b: intArray[i+2], a: intArray[i+3])    pixelArray.append(p)}

Viewing all articles
Browse latest Browse all 39

Trending Articles