1

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!

Srdjan
  • 106
  • 10
  • Have you used the debugger? What actually happens when you run the code you posted? – rmaddy Mar 23 '17 at 18:46
  • Variable `pictureData` has the length of the buffer count `1024`. I've changed the value of the count and the length of the `pictureData` is changed to the value of the count and also the runtime fails and `pictureData` line of the code is highlighted. Also, I'm sure that I'm send the image data to this method because the media is successfully upload to the server. @rmaddy – Srdjan Mar 24 '17 at 13:49
  • You need to read all of the data from the input stream, not just the first 1024 bytes. – rmaddy Mar 24 '17 at 13:54
  • Is there a way to get the size of the stream? Because I cannot predict how many bytes the stream with the image has. – Srdjan Mar 24 '17 at 13:58
  • You have to loop until there's no more data. – rmaddy Mar 24 '17 at 13:59
  • Oh yeah, so while 'hasBytesAvailable' is true read the '1024' bytes. Thanks, I'll try it. – Srdjan Mar 24 '17 at 14:01
  • I figured it out, it's working now. Do you want me to send the code so you can post the answer? Thanks for giving me idea. – Srdjan Mar 24 '17 at 14:29
  • If you think an answer will help others, feel free to post your own answer. – rmaddy Mar 24 '17 at 14:30

1 Answers1

3

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
        })
    }
Srdjan
  • 106
  • 10