English 中文(简体)
• 如何将双元数据转换为国家定位系统?
原标题:How to convert binary data to NSstring?

After recording with AVAudioRecorder the resulting sound file has to be sent to a webservice. The file data has to be URL encoded and copied into the body of the POST request. The recording format is kAudioFormatAppleLossless. As a first step, prior to URL encoding, I tried to copy the .caf file into a NSString, using

NSStringEncoding encoding;
 NSString * filestring = [NSString 
                         stringWithContentsOfURL:self.audioRecorder.url 
                     usedEncoding:&encoding 
                         error:&error];

我有以下错误:

Error territorial=NSCocoaErrorDomain Code=264 “这项行动可能已完成。 (Cocoa差错264)”

The encoding returned was 0x5bab9f0, which is not among the list of values defined for NSStringEncoding. What encoding does AVAudioRecorder use when writing to the file? What is the best way of converting a binary file to a URL encoded string?

最佳回答

Never. Use. NSString. For. Data. That s. Not. A. String... Use NSData instead, you won t need to URLEncode it (because it already is ).

你们可以这样做:

NSMutableData *postData = [[NSMutableData alloc] init];
[postData appendData:[@"--boundary
Content-Disposition: form-data; name="myfile"; filename="ad.gif"

" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithContentsOfURL:self.audioRecorder.url]];
[postData appendData:[@"
--boundary--
" dataUsingEncoding:NSUTF8StringEncoding]];

[myRequest setHTTPMethod:@"POST"]; // this you probably already did.
[myRequest setHTTPBody:postData];
问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签