I have an NSInputStream that I'm sending to server and while upload is going on, I want to display image that's uploading. Method is located in singleton and it's receiving that NSInputStream. Is there a way to read that stream and convert it to UIImage?
I've tried with this, but no luck. Where is my mistake?
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")
}
Thanks!
I've found the solution to this issue and it's in working order.
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
})
}