简体   繁体   English

我无法初始化NSInputStream

[英]I cannot initialize a NSInputStream

Please help me, im going insane. 请帮助我,我快疯了。 I need to create an NSInputStream so i can read a live preview from a camera over wifi. 我需要创建一个NSInputStream这样我就可以通过wifi从相机中读取实时预览。 liveViewStream is an NSInputStream -Instance-Variable that is declared in my implementation like so: liveViewStream是一个NSInputStream -Instance-Variable,在我的实现中声明为:

@implementation MKSonyCamHandler{
    NSInputStream *liveViewStream;
}

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). liveViewURL是一个有效的URL,当我连接到相机的网络时,我可以在浏览器中打开它(虽然我不认为这有任何区别)。 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);

The DLog after the alloc-init commands will log "(null)" every time, and ill be damned if i know why. alloc-init命令之后的DLog每次都会记录“(null)”,如果我知道为什么,该死的该死。 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? 我第一次使用NSStreams ,是否有一个常见的陷阱可能是原因? The docs clearly state that -initWithURL: 文档明确指出-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. 我开始觉得这里真的很蠢。

EDIT: i am using ARC. 编辑:我正在使用ARC。

Thanks everyone, i found it. 谢谢大家,我找到了。

The thing is, my question already had all the clues i would have needed, because, like i wrote, NSStream`s -initWithURL: will 问题是,我的问题已经有了我需要的所有线索,因为,就像我写的那样,NSStream的-initWithURL:将

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.

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. 好吧,为了它的价值,你需要创建一个CFReadStreamRef和一个CFWriteStreamRef ,然后使用魔术函数CFStreamCreatePairWithSocketToHost将它们连接到你的主机。 After that, you can cast them to NSInputStream and NSOutputStream respectively, and they will work as intended. 之后,您可以将它们分别转换为NSInputStreamNSOutputStream ,它们将按预期工作。 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:

Im upvoting your answer, since you were definitly on the right track, suggesting the use of initWithData. 我赞同你的答案,因为你肯定是在正确的轨道上,建议使用initWithData。

If you are using ARC, that might fix it: 如果您使用ARC,可能会解决它:

EDIT: Downloading it into a NSData first might fix it 编辑:首先将其下载到NSData可能会修复它

NSInputStream *tempStream;
NSData *tempData = [NSData dataWithContentsOfURL:@"Your Url"]; 
tempStream = [[NSInputStream alloc] initWithData:tempData]; 
liveViewStream = tempStream; 
DLog(@"%@", liveViewStream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM