Objective C / iOS 中 python 的 file.read() 相当于什么?

发布于 2024-12-04 05:29:51 字数 1415 浏览 0 评论 0原文

假设我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-12-11 05:29:51

我通过简单地记录字符串就找到了答案——它看起来像是十六进制格式,其中 XX 是每个字节的十六进制代码,用 ASCII 替换某些易于打印的字符。

\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00\x1d\x08\x06\x00\x00\x00\xcb\x9en\x87\x00\x00\x01\xc9IDATH\r\xc5\x971K\x03A\x10\x85s\nB \x10\xb0\n\xa4\xb2J\x95* \x08\x82\x95\xbfA\x10\xfc\x01V\xb6\x82\x95?\xc0J\x10\x04[[\xc16U@\xb0\x8d`/(\x8a\x82\x08Z\t\x12\x89\xdf$\xd9p\x9c\xae\xfb&\x9c8\xf0\xd8\xbd\xdb\xb7o^\xe6\x96\xb9K6\x1c\x0e+\xb3D\x96e\xf3\xeck\xb3\xffj\x96\xfda\xcf\\\x98\xa8#\x89\xdb\xe0\x1c\xfe\x00\xf4\x99\x0f@\x17\xec\x80\xba\xaa3\xe5Y\x05\x14\xb0\xa1\x06\x0e\xc0=\xb0\xb2\xfd\x84\x17\xee\xaf(z\x81#%72\xb1\x1bIZ4r\x03\xaf\x16\x12\xa4F\xc9\x00\x82\xf6\xbcM\xb8\x98,v\xbd\x9fJ\x1c\xd6\xd53\xd0 \xf9\x12Pc]%\xaa\x06\x9e\x11\xfcTE\xe1\xd9y\x91B2@\xb9>P{\x90\x14\xc7$\xe3K!\x19\x98(Y\x15\xd4xR\x89\x1e\x03\xef\xaa(\xbc7\x95\xeb1\xd0RE\xe1-\xab\\\x8f\x01\xcf\x19\xb8\xfb\x0b\x03\x9e\x9e\x7f\xad\x1a\x90\x1a\x915\rb\x15\xdc\x82X\xf3\t\xf7\x1f\xe1tB\xa3I\x8d\xb2\x81\x89\x89=\xc1\xc0v*i~\xdds\x06\xc8]QJ\xabpLk\x1cy7\xa99;\x16@\x1f\x84r\x17\xc7K\xe3\xa4t\xf2\xeb\xde\n\xd8;\xe1\xb7\x13n\xcd\xca^\\z\xe4\xdd\xa4\xe6\xa8^\x80\xe2\xaf.^\x9f\xa4t\xf2\xeb\xd2!$i\x13\x1c\n\xc9\x83\x993\xb8\xad|\xa2\xd8<j\x00\x01{\xde\x9b\xa0\x0b\x82\xb0w\xec\xb1w\x0bTe\x03\x90;\xc0~\xad}^y\x13\xc6\xf8\xafh\x1d\x81o\xfdaZ\x01\x167\x80\xf2\x8ccI\xd4\xfb=\xf2\xac\x85\x8a\x8c\x0cp\xe3\x14\xa8\x02e\xf1\x8e\xcd\x84\x85}>\x95%\xea\xd5iX\x1f\xf0|\xeb\x99\xe12\xa3i\x06\xfc\x7f&\xca\xb30\xaa\xc0byzn\xa5\xbaU\xe0?\rT\xbf\x00\x87\x89 \xa8s3+7\x00\x00\x00\x00IEND\xaeB`\x82

通过将图像读入 NSData,然后获取描述(这是 2 位十六进制格式字节值的列表),可以在 Objective C 中复制上述内容。之后,您可以循环遍历每个字节并根据其值决定如何表示它:

NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"anImage" ofType:@"jpg"]];
NSString *hexFormatString = [data describe];
hexFormatString = [[hexFormatString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableString * newString = [NSMutableString string];    
for (int x=0; x<[hexFormatString length]; x+=2) {
    NSString *component = [hexFormatString substringWithRange:NSMakeRange(x, 2)];
    int value = 0;
    sscanf([component cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
    if ((value <=46 && value >= 45) || (value <=57 && value >= 48) || (value <=90 && value >= 65) || (value == 95) || (value <=122 && value >= 97)) {  //48-57, 65-90, 97-122
        [newString appendFormat:@"%c", (char)value];
    }
    else {
        [newString appendFormat:@"%%%@", [component uppercaseString]];
    }
}

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.

\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00\x1d\x08\x06\x00\x00\x00\xcb\x9en\x87\x00\x00\x01\xc9IDATH\r\xc5\x971K\x03A\x10\x85s\nB \x10\xb0\n\xa4\xb2J\x95* \x08\x82\x95\xbfA\x10\xfc\x01V\xb6\x82\x95?\xc0J\x10\x04[[\xc16U@\xb0\x8d`/(\x8a\x82\x08Z\t\x12\x89\xdf$\xd9p\x9c\xae\xfb&\x9c8\xf0\xd8\xbd\xdb\xb7o^\xe6\x96\xb9K6\x1c\x0e+\xb3D\x96e\xf3\xeck\xb3\xffj\x96\xfda\xcf\\\x98\xa8#\x89\xdb\xe0\x1c\xfe\x00\xf4\x99\x0f@\x17\xec\x80\xba\xaa3\xe5Y\x05\x14\xb0\xa1\x06\x0e\xc0=\xb0\xb2\xfd\x84\x17\xee\xaf(z\x81#%72\xb1\x1bIZ4r\x03\xaf\x16\x12\xa4F\xc9\x00\x82\xf6\xbcM\xb8\x98,v\xbd\x9fJ\x1c\xd6\xd53\xd0 \xf9\x12Pc]%\xaa\x06\x9e\x11\xfcTE\xe1\xd9y\x91B2@\xb9>P{\x90\x14\xc7$\xe3K!\x19\x98(Y\x15\xd4xR\x89\x1e\x03\xef\xaa(\xbc7\x95\xeb1\xd0RE\xe1-\xab\\\x8f\x01\xcf\x19\xb8\xfb\x0b\x03\x9e\x9e\x7f\xad\x1a\x90\x1a\x915\rb\x15\xdc\x82X\xf3\t\xf7\x1f\xe1tB\xa3I\x8d\xb2\x81\x89\x89=\xc1\xc0v*i~\xdds\x06\xc8]QJ\xabpLk\x1cy7\xa99;\x16@\x1f\x84r\x17\xc7K\xe3\xa4t\xf2\xeb\xde\n\xd8;\xe1\xb7\x13n\xcd\xca^\\z\xe4\xdd\xa4\xe6\xa8^\x80\xe2\xaf.^\x9f\xa4t\xf2\xeb\xd2!$i\x13\x1c\n\xc9\x83\x993\xb8\xad|\xa2\xd8<j\x00\x01{\xde\x9b\xa0\x0b\x82\xb0w\xec\xb1w\x0bTe\x03\x90;\xc0~\xad}^y\x13\xc6\xf8\xafh\x1d\x81o\xfdaZ\x01\x167\x80\xf2\x8ccI\xd4\xfb=\xf2\xac\x85\x8a\x8c\x0cp\xe3\x14\xa8\x02e\xf1\x8e\xcd\x84\x85}>\x95%\xea\xd5iX\x1f\xf0|\xeb\x99\xe12\xa3i\x06\xfc\x7f&\xca\xb30\xaa\xc0byzn\xa5\xbaU\xe0?\rT\xbf\x00\x87\x89 \xa8s3+7\x00\x00\x00\x00IEND\xaeB`\x82

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:

NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"anImage" ofType:@"jpg"]];
NSString *hexFormatString = [data describe];
hexFormatString = [[hexFormatString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableString * newString = [NSMutableString string];    
for (int x=0; x<[hexFormatString length]; x+=2) {
    NSString *component = [hexFormatString substringWithRange:NSMakeRange(x, 2)];
    int value = 0;
    sscanf([component cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
    if ((value <=46 && value >= 45) || (value <=57 && value >= 48) || (value <=90 && value >= 65) || (value == 95) || (value <=122 && value >= 97)) {  //48-57, 65-90, 97-122
        [newString appendFormat:@"%c", (char)value];
    }
    else {
        [newString appendFormat:@"%%%@", [component uppercaseString]];
    }
}
做个少女永远怀春 2024-12-11 05:29:51

这就是您要找的吗?

NSData *data = [NSData dataWithContentsOfFile:@"filename.jpg"];
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]

Is that what you're looking for ?

NSData *data = [NSData dataWithContentsOfFile:@"filename.jpg"];
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文