RGB Data from CGImage in Swift

坚强是说给别人听的谎言 提交于 2019-12-06 07:27:11

Your code looks a bit strange. Are you sure the line:

    for var x = 0; x < Int(bitsPerPixel); x++ {

is correct? Did you really mean bitsPerPixel and not bytesPerPixel ? (or samplesPerPixel or whatever it is in swift). And are you sure a component (a sample) has 8 bits? And a pixel has no padding bytes?

Another point: what are height and width? Are they the components of the size of an image? that is wrong. You have to use pixelsWide and pixelsHigh. The size of an image says only how great --expressed in inch or centimeter or (1/72)of an inch (called a point)-- an image shall be depicted. That is independent of the number of pixels. But you work on pixels!

Change bytePointer to UnsafePointer<UInt32> and update the loop to access the individual bytes of the word using shift (>>) and mask (& 0xff).

    var pixel:UInt32 = bytePointer[row * Int(bytesPerRow) + col * Int(bytesPerPixel)]
    printString += NSString(withFormat:"(%08x)", pixel)

That'll give you the alpha channel as well, but you get the drift.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!