我有一個來自指紋傳感器設備的字節數組。我想創建一個位圖。我已經嘗試了幾個例子,但我所得到的只是一個零UIImage。如何從swift中的字節數組創建位圖圖像
如果有任何步驟可以做到這一點,請告訴我。
謝謝。
這是我的FUNC做:
func didFingerGrabDataReceived(data: [UInt8]) {
if data[0] == 0 {
let width1 = Int16(data[0]) << 8
let finalWidth = Int(Int16(data[1]) | width1)
let height1 = Int16(data[2]) << 8
let finalHeight = Int(Int16(data[3]) | height1)
var finalData:[UInt8] = [UInt8]()
// i dont want the first 8 bytes, so am removing it
for i in 8 ..< data.count {
finalData.append(data[i])
}
dispatch_async(dispatch_get_main_queue()) {() -> Void in
let msgData = NSMutableData(bytes: finalData, length: finalData.count)
let ptr = UnsafeMutablePointer<UInt8>(msgData.mutableBytes)
let colorSpace = CGColorSpaceCreateDeviceGray()
if colorSpace == nil {
self.showToast("color space is nil")
return
}
let bitmapContext = CGBitmapContextCreate(ptr, finalWidth, finalHeight, 8, 4 * finalWidth, colorSpace, CGImageAlphaInfo.Only.rawValue);
if bitmapContext == nil {
self.showToast("context is nil")
return
}
let cgImage=CGBitmapContextCreateImage(bitmapContext);
if cgImage == nil {
self.showToast("image is nil")
return
}
let newimage = UIImage(CGImage: cgImage!)
self.imageViewFinger.image = newimage
}
}
我得到一個扭曲的圖像。有人請幫助
請重新審視我的疑問句和幫助我。 –
順便說一句,寬度和高度只佔用4個字節,但你跳過前8個字節。我在下面的答案中保留了這個,但我想確保這是你的實際意圖,在你跳過的寬度和高度之後有四個字節... – Rob