2014-11-05 66 views
0

我想顯示一個PNG圖像,我通過二進制流獲取當我發送一個空字節到服務器。 我確實從流中獲取信息。但圖像從未顯示,我不知道爲什麼。也許有人看到一個錯誤。從二進制流顯示圖像

我的代碼如下:

#import <UIKit/UIKit.h> 
NSInputStream *inputStream; 
NSOutputStream *outputStream; 
@interface ViewController : UIViewController <NSStreamDelegate> 
@property (weak, nonatomic) IBOutlet UIView *secondView; 
@property (weak, nonatomic) IBOutlet UIImageView *imageView; 

- (IBAction)sendNullbyte:(id)sender; 
@property (weak, nonatomic) IBOutlet UIView *firstView; 

@property (nonatomic, strong, readwrite) NSOutputStream *fileStream; 

@end 

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self initNetworkCommunication]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

- (void)initNetworkCommunication { 
    CFReadStreamRef readStream; 
    CFWriteStreamRef writeStream; 
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 80, &readStream, &writeStream); 
    inputStream = (__bridge NSInputStream *)readStream; 
    outputStream = (__bridge NSOutputStream *)writeStream; 
    [inputStream setDelegate:self]; 
    [outputStream setDelegate:self]; 
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [inputStream open]; 
    [outputStream open]; 
} 

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent{ 
    switch (streamEvent) { 
     case NSStreamEventOpenCompleted: 
      NSLog(@"Stream opened"); 
      break; 
     case NSStreamEventHasBytesAvailable: 
      NSLog(@"Has bytes available."); 
      //be sure that event comes from input stream 
      if(theStream == inputStream){ 
       UInt8 buffer[500000]; 
       long len; 
       BOOL firstbytes = true; 
       //read method returns 0 when there is nothing left in the stream 
       len = [inputStream read:buffer maxLength:sizeof(buffer)]; 
       if (len > 0){ 
        NSData *output = [[NSData alloc] initWithBytes:buffer length:len-4]; 
        if (output != nil){ 
         NSLog(@"server said: %@", output); 
         UIImage *image = [[UIImage alloc] initWithData:output];; 
         _imageView.image = image; 
        } 
       } 
      } 

      break; 
     case NSStreamEventErrorOccurred: 
      NSLog(@"Cant not connect to the host"); 
      break; 
     case NSStreamEventEndEncountered: 
      break; 
     default: 
      break; 
    } 
} 

- (IBAction)sendNullbyte:(id)sender { 
    NSInteger nullbyte = 0x00; 
    NSData *data =[[NSData alloc] initWithBytes:&nullbyte length:1]; 
    [outputStream write:[data bytes] maxLength:[data length]]; 
    [self.view bringSubviewToFront:_secondView]; 
} 

@end 

回答

1

嘗試使用unit8_t代替UInt8,並確保ü處理,圖像沒有得到到u的情況下單個數據包...

U可以這樣做:

{ 
    NSMutableData *data; // ivar 
} 

    // init it somewhere 
    data = [NSMutableData new]; 

case NSStreamEventHasBytesAvailable: 

if (theStream == inputStream) { 

    uint8_t buffer[5000]; 
    int length; 

    while ([inputStream hasBytesAvailable]) { 
     length = [inputStream read:buffer maxLength:sizeof(buffer)]; 
     if (length > 0) { 
      [data appendBytes:(const void *)buffer length]; 
     } 
    } 
} 
break; 

case NSStreamEventEndEncountered: 
{ 
    if (theStream == inputStream) { 
     UIImage *imagess = [[UIImage alloc]initWithData:data]; 
     [imagesview setImage:imagess]; 
    } 
} break; 
+0

...我試着用uint8_t代替和while循環 - 沒有成功 我不能使用NSStreamEventEndEncountered的情況,因爲流保持打開... – LilK3ks 2014-11-05 15:38:10

+0

爲什麼?你應該得到'NSStreamEventEndEncountered', – gran33 2014-11-06 07:57:12