首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为NSOutputStream添加数据?

如何为NSOutputStream添加数据?
EN

Stack Overflow用户
提问于 2009-03-29 13:02:15
回答 1查看 5.4K关注 0票数 1

我想将UIImage转换为NSOutputStream,并通过socket将其发送到服务器。

代码语言:javascript
复制
#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:

代码语言:javascript
复制
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-03-29 13:37:07

我的问题是,我可以在哪里添加像"sendStream = ...“这样的代码?

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

…如何将imageData转换为NSOutputStream的实例?

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/694601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档