gpt4 book ai didi

ios - 如何将 NSInputStream 转换为 UIImage

转载 作者:搜寻专家 更新时间:2023-11-01 06:34:34 38 4
gpt4 key购买 nike

我有一个要发送到服务器的 NSInputStream,在上传过程中,我想显示正在上传的图像。方法位于单例中,它正在接收 NSInputStream。有没有办法读取该流并将其转换为 UIImage?

我试过这个,但没有成功。我的错误在哪里?

var buffer = [UInt8](count:1024, repeatedValue: 0)
thumbnailStream.open()
if (thumbnailStream.hasBytesAvailable){
let len : Int = inputStream.read(&buffer, maxLength: buffer.count)
var pictureData = NSData(bytes: buffer, length: len)
var imagess = UIImage(data: pictureData)!
self.headerProgress.imageView.image = imagess
print("Data of image = \(pictureData.length)")

}else{
print("No bytes")
}

谢谢!

最佳答案

我已经找到了这个问题的解决方案,并且可以正常工作。

    var count = 0
let dataOfTheImage = NSMutableData()
var buffer = [UInt8](count:1024, repeatedValue: 0)
thumbnailStream.open()

//MARK: Loop through thumbnail stream
while (thumbnailStream.hasBytesAvailable){
count = count + 1
print("Counter: \(count)")
//MARK: Read from the stream and append bytes to NSMutableData variable
let len = thumbnailStream.read(&buffer, maxLength: buffer.count)
dataOfTheImage.appendBytes(buffer, length: len)

//MARK: Size of the image in MB
let size = Float(dataOfTheImage.length) / 1024.0 / 1024.0
print("Data length = \(size)")
}

//MARK: Check if there are no bytes left and show the image
if (thumbnailStream.hasBytesAvailable == false){
thumbnailStream.close()
// var pictureData = NSData(bytes: buffer, length: len)
let progressImage = UIImage(data: dataOfTheImage)!
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.headerProgress.imageView.image = progressImage
})
}

关于ios - 如何将 NSInputStream 转换为 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42984120/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com