The docs for NSURL
state that:
NSURL的文档说明:
An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data.
NSURL对象表示一个URL,该URL可能包含远程服务器上资源的位置、磁盘上本地文件的路径,甚至是任意编码的数据。
I have a blob of in-memory data that I'd like to hand to a library that wants to load a resource via an NSURL
. Sure, I can first write this NSData
to a temp file and then create a file://
NSURL
from that, but I'd prefer to have the URL point directly to the buffer that I already have present in memory.
我有一团内存中的数据,我想把它们交给一个想通过NSURL加载资源的库。当然,我可以先将这个NSData写到一个临时文件中,然后从中创建一个文件:// NSURL,但是我更希望URL点直接指向内存中已经存在的缓冲区。
The docs quoted above seem to suggest this is possible, but I can't find any hint of how to accomplish it. Am I missing something?
上面引用的文档似乎表明这是可能的,但是我找不到任何关于如何实现它的提示。我遗漏了什么东西?
2 个解决方案
#1
25
NSURL
supports the data:// URL-Scheme (RFC 2397).
This scheme allows you to build URLs in the form of
NSURL支持数据:// URL-Scheme (RFC 2397)。该方案允许您以表单的形式构建url。
data://data:MIME-Type;base64,<data>
A working Cocoa example would be:
一个可用的可可示例是:
NSImage* img = [NSImage imageNamed:@"img"];
NSData* imgData = [img TIFFRepresentation];
NSString* dataFormatString = @"data:image/png;base64,%@";
NSString* dataString = [NSString stringWithFormat:dataFormatString, [imgData base64EncodedStringWithOptions:0]];
NSURL* dataURL = [NSURL URLWithString:dataString];
Passing around large binary blobs with data URLs might be a bit inefficient due to the nature of base64 encoding.
由于base64编码的特性,使用数据url传递大型二进制blobs可能有点低效。
You could also implement a custom NSURLProtocol that specifically deals with your data. Apple has some sample code that uses a custom protocol to pass around image objects: https://developer.apple.com/library/mac/samplecode/SpecialPictureProtocol/Introduction/Intro.html#//apple_ref/doc/uid/DTS10003816
还可以实现专门处理数据的自定义NSURLProtocol。苹果有一些使用自定义协议来传递图像对象的示例代码:https://developer.apple.com/library/mac/samplecode/specialpicturetionprotocol / introduction / intro.html #/ apple_ref/doc/uid/DTS10003816
#2
5
What you are missing is the NSURLProtocol class. Takes about three dozen lines of code, and any code that handles URLs properly can access your in-memory data. Read the documentation, it's not difficult and there is sample code available.
您所缺少的是NSURLProtocol类。大约需要36行代码,任何正确处理url的代码都可以访问内存中的数据。阅读文档,这并不困难,并且有可用的示例代码。
Unfortunately there are some APIs that take an NSURL as a parameter, but can only handle file URLs.
不幸的是,有些api以NSURL作为参数,但只能处理文件url。
#1
25
NSURL
supports the data:// URL-Scheme (RFC 2397).
This scheme allows you to build URLs in the form of
NSURL支持数据:// URL-Scheme (RFC 2397)。该方案允许您以表单的形式构建url。
data://data:MIME-Type;base64,<data>
A working Cocoa example would be:
一个可用的可可示例是:
NSImage* img = [NSImage imageNamed:@"img"];
NSData* imgData = [img TIFFRepresentation];
NSString* dataFormatString = @"data:image/png;base64,%@";
NSString* dataString = [NSString stringWithFormat:dataFormatString, [imgData base64EncodedStringWithOptions:0]];
NSURL* dataURL = [NSURL URLWithString:dataString];
Passing around large binary blobs with data URLs might be a bit inefficient due to the nature of base64 encoding.
由于base64编码的特性,使用数据url传递大型二进制blobs可能有点低效。
You could also implement a custom NSURLProtocol that specifically deals with your data. Apple has some sample code that uses a custom protocol to pass around image objects: https://developer.apple.com/library/mac/samplecode/SpecialPictureProtocol/Introduction/Intro.html#//apple_ref/doc/uid/DTS10003816
还可以实现专门处理数据的自定义NSURLProtocol。苹果有一些使用自定义协议来传递图像对象的示例代码:https://developer.apple.com/library/mac/samplecode/specialpicturetionprotocol / introduction / intro.html #/ apple_ref/doc/uid/DTS10003816
#2
5
What you are missing is the NSURLProtocol class. Takes about three dozen lines of code, and any code that handles URLs properly can access your in-memory data. Read the documentation, it's not difficult and there is sample code available.
您所缺少的是NSURLProtocol类。大约需要36行代码,任何正确处理url的代码都可以访问内存中的数据。阅读文档,这并不困难,并且有可用的示例代码。
Unfortunately there are some APIs that take an NSURL as a parameter, but can only handle file URLs.
不幸的是,有些api以NSURL作为参数,但只能处理文件url。