gpt4 book ai didi

ios - 显示二进制流中的图像

转载 作者:行者123 更新时间:2023-11-29 02:31:52 25 4
gpt4 key购买 nike

当我向服务器发送空字节时,我想显示通过二进制流获得的 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

最佳答案

尝试使用 unit8_t 而不是 UInt8,并确保您处理图像无法在单个数据包中到达您的情况...

你可以这样做:

{
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;

关于ios - 显示二进制流中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26759591/

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