【发布时间】:2012-10-08 16:44:23
【问题描述】:
我正在尝试创建一个包含主包中文件内容的 NSData 对象。
NSString *chordPath = [[NSBundle mainBundle] pathForResource:chordName ofType:@"png"];
NSURL *chordURL = [NSURL URLWithString:[chordPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL];
UIImage *chordImage = [UIImage imageWithData:chordImageData];
在使用 stringByAddingPercentEscapesUsingEncoding 之前,我得到了一个 nil URL,所以我继续通过重新编码字符串来修复它。现在我得到了一个有效的 URL,但 chordImageData 对象对我来说是 nil。该文件肯定包含在我的主包中(因为我能够获取开始的 URL)所以我想知道出了什么问题。
编辑:
运行这个:
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL options:NSDataReadingMapped error:&dataCreationError];
给我这个错误:
po dataCreationError
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7530a00
环顾 Google,似乎该 URL 仍未正确编码。有人知道确保编码有效的额外步骤吗?
【问题讨论】:
-
您可以查看
dataWithContentsOfURL:options:error:获取数据时是否出现错误。 -
是的,只是这样做了,错误已发布。谢谢。