How to get stream bytes and convert back to image, below is my code. But the images I get is blank. Why? Something wrong of the code?
I get the bytes from my NSOutputStream and convert back to NSData, then convert the NSData to image.
(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream) {
uint8_t buffer[5000];
int len;
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
NSLog(@"len=%d", len);
if (len > 0) {
NSData *pictureData = [NSData dataWithBytes:buffer length:len];
UIImage *imagess = [[UIImage alloc]initWithData:pictureData];
[imagesview setImage:imagess];
}
}
}
break;
}
}