簡體   English   中英

如何在Swift中將UIimage更改為二進制數據

[英]How can I change UIimage to binary data in Swift

我想通過將信息解碼為字節(UInt8)數組將圖像數據(捕獲快照)發送到Python服務器,但效果不佳:(

我的代碼在這里

typealias Byte = UInt8

let captureImage:UIImage = self.sceneView.snapshot()

//change UIImage to binary data

let imageData: NSData = UIImageJPEGRepresentation(captureImage, 0)! as NSData

let AR_IMG_WIDTH = Int(captureImage.size.width) //750
let AR_IMG_HEIGHT = Int(captureImage.size.height) //1334
let AR_BUFFER_SIZE = AR_IMG_WIDTH * AR_IMG_HEIGHT * 3 + 6 //3001506 , 3(R,G,B),6 (MMS+MME)

print("size of imageData 750 * 1334 * 3 : \(imageData.length)")

//NSData to [Byte]
var mms: [Byte] = [Byte]("MMS".utf8) // 3Byte
let mme: [Byte] = [Byte]("MME".utf8) // 3Byte
var copyArray = [Byte](repeating: 0, count: imageData.length)
imageData.getBytes(&copyArray, length: imageData.length)
mms.append(contentsOf: copyArray)
mms.append(contentsOf: mme)

我的問題是

1)為什么imageData.length(byte的大小)值是可變的? 我想這個值必須是ImageWidth * ImageHeight * 3(R,G,B data)+ 6(MMS,MME)字節

2)如果我想傳輸圖像信息,請使用其他函數或類?

讓我回答你的問題1)

像許多其他圖像格式一樣,JPEG壓縮RGB圖像數據。 這意味着字節數不超過寬度X高度X 3,而是更少。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM