2

I'm trying to create an image (8-bit greyscale bitmap ?) from a dataset that I have acquired (it features x-y-coordinates and the actual pixel-data-information 0-255).

Now I would like to do that by using Apple's Cocoa/Foundation/etc. frameworks in Swift, but I just can't seem to grasp the idea of how it is even possible.

I really don't need to do any additional image-manipulation other than creating this simple greyscale-image.

So basic workflow would look like this:

  1. initialize image by size as 8-bit greyscale
  2. fill each pixel with the appropriate information from the array
  3. save the image into a file

2 Answers 2

2

Here's a snippet of code that I slapped together which does actually save a bmp-file with my data. So it does work. Absolutely not good code, I assume. But I was just trying to get a working example, which I would refine later.

var myImageDataArray : [UInt8] = [167, 241, 217, 42, 130, 200, 216, 254, 67, 77, 152, 85, 140, 226, 179, 71]
let dataProv = CGDataProviderCreateWithData(nil, myImageDataArray, myImageDataArray.count, nil)
let myCG = CGImageCreate(4, 4, 8, 8, 4, CGColorSpaceCreateWithName(kCGColorSpaceGenericGrayGamma2_2), CGBitmapInfo.ByteOrderDefault, dataProv, nil, false, CGColorRenderingIntent.RenderingIntentDefault)
let testTIFF = NSBitmapImageRep(CGImage: myCG!)
let mynewData = testTIFF.representationUsingType(NSBitmapImageFileType.NSTIFFFileType, properties: [NSImageCompressionMethod: 1])
mynewData?.writeToFile("testTIFF.tiff", atomically: false)
Sign up to request clarification or add additional context in comments.

Comments

0

If you have a [UInt8] representation of the image your code will look like this:

let imgBytes = // image array [UInt8]
let imgData = NSData(bytes: imgBytes, length: imgBytes.count)
UIImage(data: imgData)

1 Comment

Thank you for the suggestion. I started to think a little differently about this problem and I managed to get kind of what I wanted now. See below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.