gpt4 book ai didi

objective-c - 如何为NSOutputStream添加数据?

转载 作者:行者123 更新时间:2023-12-03 17:08:43 25 4
gpt4 key购买 nike

我想将 UIImage 转换为 NSOutputStream 并通过套接字将其发送到服务器。


#import "Connection.h"

@implementation Connection

-(void) open: (NSString *) h : (int) p
{
strHost = h;
intPort = p;

[NSStream getStreamsToHost:objHost
port:intPort
inputStream:&receiveStream
outputStream:&sendStream];

[receiveStream retain];
[sendStream retain];

[receiveStream setDelegate:self];
[sendStream setDelegate:self];

[receiveStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[sendStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[receiveStream open];
[sendStream open];

printf("Open.\n");
}


- (void) stream: (NSStream *) stream handleEvent: (NSStreamEvent) eventCode
{
printf("EVENT: Start.\n");

switch(eventCode)
{
case NSStreamEventOpenCompleted:
{
printf("EVENT: Open completed.\n");

if(stream == receiveStream)
{
printf("Receiving...\n");
}

if(stream == sendStream)
{
printf("Sending...\n");

NSString * strBuffer = [NSString stringWithFormat:@"GET / HTTP/1.0\r\n\r\n"];
const uint8_t * rawstring = (const uint8_t *)[strBuffer UTF8String];

[sendStream write:rawstring maxLength:strlen(rawstring)];
}

break;
}
case NSStreamEventEndEncountered:
{
printf("EVENT: End encountered.\n");
break;
}
case NSStreamEventHasSpaceAvailable:
{
printf("EVENT: Has space available.\n");
break;
}
case NSStreamEventHasBytesAvailable:
{
printf("EVENT: Has bytes available.\n");
break;
}
case NSStreamEventErrorOccurred:
{
printf("EVENT: Error occurred.\n");
break;
}
case NSStreamEventNone:
{
printf("EVENT: None.\n");
break;
}
}

printf("EVENT: End.\n");
}

-(void) close
{
[receiveStream close];
[sendStream close];

printf("Closed.\n");
}

@end


我的问题是在哪里可以添加“sendStream = ...”之类的代码?

另一个问题是我可以使用以下方法将 UIImage 转换为 NSData:

NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);

但是如何将imageData转换为NSOutputStream的实例呢?

最佳答案

My question is where can I add code like "sendStream = ..."?

您已使用 getStreamsToHost:port:inputStream:outputStream: 消息分配 sendStream。该方法通过引用返回两个流。

… how to convert the imageData to NSOutputStream's instance?

您不需要将数据转换为流,您需要告诉流写入数据。

尝试NSOutputStream's write:maxLength: method 。您需要传递数据对象的字节和长度。

关于objective-c - 如何为NSOutputStream添加数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/694601/

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