convert AVAudioRecorder to NSString or bytes - ios

I am trying to send AVAudioRecorder in Json
,
i would like to convert it to NSString or Byte..
any idea?

Never tried but maybe this ?
// Get the recording
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:filePath settings:recordSetting error:&error];
....
// Convert to NSData
NSData *theData = [[NSData alloc] initWithContentsOfFile:filePath];
// Convert to NSString
NSString* dataAsString = [NSString stringWithUTF8String:[theData bytes]];

Once you have completed a recording with AVAudioRecorder, you can retrieve the recorded file using your AVAudioRecorder's url property. Then you can initialize an NSData with the url:
NSData *data = [NSData dataWithContentsOfURL:recorder.url];
To send it as JSON, you need to base64 the data. I recommend this project which has a category for doing this: https://github.com/l4u/NSData-Base64
You'll need to #import "NSData+Base64.h" into your source and then you can get the base64 string from your NSData by calling base64EncodedString on it.

Related

How to convert NSData to an audio file and save it?

How to create audiofile.aif and append audio with nsdata?
NSData * audioData = [NSData dataWithBytes:inBuffer->mAudioData length:inBuffer->mAudioDataByteSize * NUM_BUFFERS];
For Convert NSData
myData = [NSData dataWithContentsOfURL:myUrl]; //"audiofile.aif" url
Use NSData writeToFile method for save file.
Try this
[myData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/audio.aif"] atomically:YES];

Unable to Convert XMLData into Dictionary Form

I have an XML file and I am converting the data into NSString using this code:-
NSString *strPath=[[NSBundle mainBundle] pathForResource:#"VISG" ofType:#"xml"];
NSData *data = [NSData dataWithContentsOfFile:strPath];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
The problem I am facing is I am getting the string with all the XMLValues but I am unable to convert that string into Dictionary. I have used XMLReader but it always returns me nil . I have tried the below two methods for converting the string which I ma getting from NSdata but it always throws nil
dict = [XMLReader dictionaryForXMLString:string
options:XMLReaderOptionsProcessNamespaces
error:&error];
dict = [XMLReader dictionaryForXMLString:string error:nil]
Can anyone please tell me what is the problem. Or can you please tell me some more libraries other than XMLReader for parsing the XMLfile.

NSData getting trucated while converting from Pdf

I am trying to upload a pdf from iClouds in iPhone in Objective C. The following is my code which gets triggered on click.
UIDocumentPickerViewController *doc = [[UIDocumentPickerViewController alloc]initWithDocumentTypes:#[#"public.composite-content"] inMode:UIDocumentPickerModeImport];
doc.delegate = (id)self;
doc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:doc animated:YES completion:nil];//getting the path of the file selected through didPickDocumentAtURL
NSString *path = [url path];//converting in to data in two different ways but getting truncated nsdata
NSData *data3 = [[NSFileManager defaultManager] contentsAtPath:path];//Truncating
NSData *data1 = [NSData dataWithContentsOfFile:path];//Truncating Both data3 and data1 are getting truncated after 32767 characters.
Questions:
Is this correct format for importing a .pdf into NSData?
Is there any limit for NSData? (I am asking this because it's getting truncated exactly after 32767 characters)
I solved it by calling the delegate method of UIDocumentPickerViewController
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url{
if (controller.documentPickerMode== UIDocumentPickerModeImport) {
//taking the path of the file selected
NSString *path = [url path];
//converting to NSData
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
//converting to base64 string
base64String = [data base64EncodedStringWithOptions:kNilOptions];
}
}

How to convert NSData with textEncoding utf-8 into NSURL

I want to convert NSData (textEncoding utf-8) into NSURL.
I am writing below code for this, but conversion from NSData to NSString returning nil. (May be due to encoding type)
NSString *stringFromData = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; // stringFromData is nil after execution of this line.
NSURL *url = [[NSURL alloc] initWithString:stringFromData];
So what should I do to convert NSData into NSURL in my case.
But when I am trying to load this data into webview, Its working good. Here is my code to load this data into webview.
[self.webView loadData:myData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil]; //PDF is showing in my webview.
but I am not able to convert this NSData into NSURL. What encoding should I use to convert NSData with textEncoding utf-8 into NSURL ?
You're trying to load data that represents a PDF into an NSString. A PDF file does not consist of UTF-8 encoded characters that represent text, it's a file that contains header information, fonts, vector graphics AND text.
The only solution to your problem in my mind is change the source of the NSData to something that will provide UTF-8 encoded characters that make up a URL.
If you can't get data any other way, why not check if you can extract the textual data from the PDF? https://github.com/zachron/pdfiphone
Have you tried writing the data to local file,
NSString *docPath = [NSHomeDirectory() stringByAppendingString:#"/Documents"];
NSString *pdfFilePath = [docPath stringByAppendingPathComponent:#"pdfFile.pdf"];
BOOL success = [myData writeToFile:pdfFilePath atomically:YES];
if (success) {
NSURL *url = [[NSURL alloc] initFileURLWithPath:pdfFilePath];
NSLog(#"url from data : %#",url);
}
Hope this helps.
Thanks
Ok, try this:
//Convert data to string
NSString *urlString = [[NSString alloc] initWithData:self encoding:NSUTF8StringEncoding];
//Convert the string to URL
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Objective-c Base64 NSString to NSData conversion

I am trying to load in a very large string that is a base64 encoded PNG into NSData to create a UIImage on the fly. I can get the image generated by it is very distorted. Am I doing this correctly? I am also using SBJson in this example.
// Data is the NSData loaded in from the web
NSString *responseValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *successData = [(NSDictionary*)[responseValue JSONValue] objectForKey:#"MapFlightResult"];
NSData *pngData = [[NSData alloc] initWithBase64EncodedString:successData options:1];
UIImage *map = [UIImage imageWithData:mapData];
[imageView setImage:map];
I believe you issue is that your not sending in a base64 encoded string to initWithBase64EncodedString.

Resources