我无法初始化NSInputStream [英] I cannot initialize a NSInputStream

查看:117
本文介绍了我无法初始化NSInputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,我快疯了。
我需要创建一个 NSInputStream ,以便可以通过wifi从摄像机读取实时预览。
liveViewStream是一个 NSInputStream -Instance-Variable,在我的实现中声明为:

Please help me, im going insane. I need to create an NSInputStream so i can read a live preview from a camera over wifi. liveViewStream is an NSInputStream-Instance-Variable that is declared in my implementation like so:

@implementation MKSonyCamHandler{
    NSInputStream *liveViewStream;
}

liveViewURL 是一个连接到相机网络时可以在浏览器中打开的有效URL(尽管我认为这没有任何区别)。我已经检查过它的存在,不是nil并保持我期望的值。
但是当我这样做时:

liveViewURL is a valid URL that i can open in my browser when i connect to the camera's network (although i dont think that this makes any difference). I have checked that it exists, is not nil and holds the value that i expect. But when i do this:

liveViewStream = [[NSInputStream alloc] initWithURL:liveViewURL];
DLog(@"%@", liveViewStream);

在alloc-init命令之后的DLog每次都会记录(null),并且该死的,如果我知道为什么。有人遇到过吗?我在这里缺少明显的东西吗?
这是我第一次与 NSStreams 一起工作,这是否可能是一个常见的陷阱?
文档明确指出-initWithURL:

The DLog after the alloc-init commands will log "(null)" every time, and ill be damned if i know why. Has anybody ever encountered this? Am i missing something glaringly obvious here? Its my first time working with NSStreams, is there a common pitfall that might be the reason? The docs clearly state that -initWithURL:

Creates and returns an initialized NSInputStream object that reads data from
the file at a given URL.

有什么想法吗?我在这里开始感到自己很愚蠢。

Any ideas? Im starting to feel really stupid here.

编辑:我正在使用ARC。

i am using ARC.

推荐答案

谢谢大家,我找到了。

问题是,我的问题已经有了我需要的所有线索,因为就像我写的那样,NSStream `s -initWithURL:将

The thing is, my question already had all the clues i would have needed, because, like i wrote, NSStream`s -initWithURL: will

Create and return an initialized NSInputStream object that reads data from
the file at a given URL.

我没看到的是,这仅适用于本地资源。如果您要使用远程主机(我有无线网络连接),则需要使用其他主机,因为我在这里再次引用了文档:

What I didn't see is that this is only true for local sources. If you want a remote host, (i had a wireless network connection) you need to use something else, because, and i quote the docs again here:

The NSStream class does not support connecting to a remote host on iOS.

那么,对于它的价值,您需要创建一个 CFReadStreamRef CFWriteStreamRef ,然后使用魔术函数 CFStreamCreatePairWithSocketToHost 将它们连接到主机。之后,您可以将它们分别转换为 NSInputStream NSOutputStream ,它们将按预期工作。以下是文档中的代码示例:

Well, for what its worth, you need to create a CFReadStreamRef and a CFWriteStreamRef, and then use the magic function CFStreamCreatePairWithSocketToHost to connect them to your host. After that, you can cast them to NSInputStream and NSOutputStream respectively, and they will work as intended. Heres a code example from the docs:

    CFReadStreamRef readStream;

    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 80, &readStream, &writeStream);



    NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;

    NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;

    [inputStream setDelegate:self];

    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];

    [outputStream open];

希望这对某人有所帮助。

Hope this helps someone at some point.

@leparlon:

@leparlon:

我赞成您的回答,因为您肯定在正确的轨道上,建议使用initWithData。

Im upvoting your answer, since you were definitly on the right track, suggesting the use of initWithData.

这篇关于我无法初始化NSInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆