Objective C / iOS 中 python 的 file.read() 相当于什么?
假设我有 Python 代码:
someString = file("filename.jpg").read()
如何在 iOS 上的 Objective C 中复制这行代码?我正在尝试将 jpg 转换为字符串,以进行最终的 url 编码,以便我可以将其传递给 Tumblr API。我最初尝试使用 Base64 编码将图像转换为字符串,但这似乎没有从 tumblr 获得有效响应。我也尝试过 NSUTF8Encoding,但这些尝试返回 nil 字符串(见下文)。
我为什么要这样做?来自 Tumblr 的某人在 回复到此线程,该过程似乎从使用 file.read() 将图像转换为字符串开始。遗憾的是,该 API 不接受简单的“multipart/form-data”请求,仅接受“application/x-www-form-urlencoded”格式。
Python 使用什么类型的编码来执行此 read(),以及如何在 iPhone 上的 Objective C 中复制它?
我尝试了以下操作:
NSString *utf8String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"jpg"] encoding:NSUTF8StringEncoding error:&error];
这给出了一个nil
字符串和错误261操作无法完成。
使用base64编码给了我一个字符串,但Tumblr API没有' url 编码后不接受它,所以我认为它没有做 Python 使用 read() 所做的事情(尽管我可能只是错过了一个步骤)。 Tubmlr API 响应 "status":400,"msg":"Bad Request"},"response":{"errors":["Error uploading photo."]
有一个类似的问题在 stackoverflow 上关于 在此处将照片上传到 Tumblr 上的照片集,但是这个问题更具体到发布的Python代码。
Say that I have the Python code:
someString = file("filename.jpg").read()
How can I replicate this line of code in Objective C on iOS? I'm trying to convert a jpg into a string, for eventual url encoding so I can pass it on to the Tumblr API. I tried base64 encoding to convert the image to a string initially, but that doesn't seem to get a valid response from tumblr. I also tried NSUTF8Encoding, but those attempts return nil strings (see below).
Why am I doing this? Someone from Tumblr posted a Python example of how to submit multiple photos for a photoset in response to this thread, and the process appears to start with converting the images to strings using file.read(). The API unfortunately doesn't take a simple ‘multipart/form-data’ request, only ‘application/x-www-form-urlencoded’ format.
What sort of encoding is Python using to do this read(), and how can I replicate it in Objective C on the iPhone?
I have tried the following:
NSString *utf8String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"jpg"] encoding:NSUTF8StringEncoding error:&error];
This gives a nil
string and error 261 The operation couldn’t be completed.
Using base64 encoding gives me a string, but the Tumblr API doesn't accept it after url encoding it, so I assume it's not doing what Python is doing with read() (though I could just be missing a step). The Tubmlr API responds with "status":400,"msg":"Bad Request"},"response":{"errors":["Error uploading photo."]
There is a similar question on stackoverflow about uploading photos to photosets on Tumblr here, but this question is more specific to the posted Python code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过简单地记录字符串就找到了答案——它看起来像是十六进制格式,其中 XX 是每个字节的十六进制代码,用 ASCII 替换某些易于打印的字符。
通过将图像读入 NSData,然后获取描述(这是 2 位十六进制格式字节值的列表),可以在 Objective C 中复制上述内容。之后,您可以循环遍历每个字节并根据其值决定如何表示它:
I figured the answer out by simply logging the string -- it looks like it's a hexadecimal format, with \xXX where XX is the hexadecimal code for each byte, with ASCII substitutions for certain easily-printed characters.
The above can be replicated in Objective C by reading in the image to NSData, and then getting the description (which is a list of 2 digit hexadecimal-formatted byte values). After that, you could loop through each byte and decide how to represent it based on its value:
这就是您要找的吗?
Is that what you're looking for ?