Obtain NSImage from Pixel Array Problems (Swift)
<p>L.E : I've found the issue, now it does'n crashing anymore, but now ( new issue ) it doesn't draw the image as it should.. It is damaged...</p>

<p>I have a pixel array I need to turn into a CGImage and then into a NSImage. I've tried this version of a code used on ios for obtaining aUIImage and I get an error I can not handle: <strong>CGImageCreate: invalid image bits/pixel or bytes/row.
fatal error: unexpectedly found nil while unwrapping an Optional value</strong></p>

<p>Any idea, any help will be honestly appreciated! Thank you! I'll let the code here : </p>

<pre><code> func imageFromPixels(image : CGImage, size:NSSize,pixels: UnsafeMutablePointer&lt;UInt8&gt;, width: Int, height: Int)-&gt; NSImage {
    let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo:CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
   let bitsPerComponent = CGImageGetBitsPerComponent(image)
    let bitsPerPixel = CGImageGetBitsPerPixel(image)
    let bytesPerRow = CGImageGetBytesPerRow(image)
    var data = pixels
    let providerRef = CGDataProviderCreateWithCFData(
        NSData(bytes: &amp;data, length: height * width  * sizeof(UInt8))
    )

    let cgim = CGImageCreate(
        width,
        height,
        bitsPerComponent,
        bitsPerPixel,
        bitsPerRow,
        rgbColorSpace,
        bitmapInfo,
        providerRef,
        nil,
        true,
        .RenderingIntentDefault
    )
    return NSImage(CGImage: cgim!, size: size)
}
</code></pre>