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];
Related
I'm converting an image that I retrieved from a URL to base 64 using this code.
NSURL* imageUrl = [NSURL URLWithString:url];
NSData* urlData = [NSData dataWithContentsOfURL:imageUrl];
UIImage* uiImage = [UIImage imageWithData: urlData];
NSData* imageData = UIImagePNGRepresentation(uiImage);
NSData* base64 = [imageData base64EncodedDataWithOptions:0];
return [NSString stringWithUTF8String:[base64 bytes]];
the image url: https://api.qrserver.com/v1/create-qr-code/?data=somedata&size=220x220&margin=0
the generated base64 image.
iVBORw0KGgoAAAANSUhEUgAAANwAAADcCAMAAAAshD+zAAAABlBMVEX///8AAABVwtN+AAAAHGlET1QAAAACAAAAAAAAAG4AAAAoAAAAbgAAAG4AAASMOw2BmAAABFhJREFUeAHsmdFq5VYQBO3//+lADqgKUZcjpNU6xLNP5e7puVLPTWI2X99/6s9X/WF5uV/Y3/gSH6L3P1vF04lYKRHEnpdTF5dwLnetJr5tEEk0EfZ8LdXFJZyv5bWa9HU7kOQhGbDna6kuLuFv+Vr6+3KVVSCRFLHVp0RwF2cyyXE4Rzci6f6HJuOENrZ25mSKbP/e1Zh5RG861BQP92v3kbu4NhU6DtfkTiOtllPUJnyJILZ2Yu/IcXiXKp+0HiRFpfElgtjaib0jx+FdqnzSepAUlcaXCGJrJ/aOHId3qfJJ60FSVBpfIoitndg7chzepconrQdJUWl8iSC2dmLvyHF4lyqftB4kRaXxJYLY2om9I8fhXap80nqQFJXGlwhiayf2jhyHSaE15SSiiLzEDZLRy0lMZKfs/C+q/MTehAoRR9sRmXm51QWNuRs4fWxRTiKKCEncIJm53OqCwtwNnD62KCcRRYQkbpDMXG51QWHuBk4fW5STiCJCEjdIZi63uqAwdwOnjy3KSUQRIYkbJDOXW11QmLuB08cW5eRGxM5fitL+L14ua8inlwhmXGJixxlNH1uUkxsRey63qqQRVZuYk1ljxiX2pkPNycO991d72pnIetnzcirjX+yaDlXjh2bAt3ow9vzbcnVxVKOvYv91urpL7E2HqsyhGfCtHow9l1tdHNW8fTk+SKR7JDIqO0X5YE7qRdMnLspJxCTFEwnJTlE+mJPzchS0qGtCLTrvOP9MRk6K8sGcnMtR0KKuCbXovOP8Mxk5KcoHc3IuR0GLuibUovOO889k5KQoH8zJuRwFLeqaUIvOO84/k5GTonwwJ3/f5ahhR1T3+Jfc46Ny5+FuwXF4G4sB0vNyqwtK2nXD5HXyTvh6nknSc7n
I figured out the image is only halved because I checked using this tool.
http://codebeautify.org/base64-to-image-converter
Is there anyway I can generate a base64image String that contains the whole image?
In terms of why it's getting cut off, I suspect you're looking at the base64 string in the debugger, which will truncate it. Actually NSLog the string and you'll see it's longer than what you're seeing in the debugger.
A couple of other unrelated observations:
You should not use stringWithUTF8String with [base64 bytes] because the NSData will not be null terminated. If you really needed to convert it to a string, you'd use initWithData rather than stringWithUTF8String:
return [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
As others have pointed out, you can bypass the creation of the NSData of the base64 altogether, and create the string directly:
return [imageData base64EncodedStringWithOptions:0];
I'm not sure why you're taking the NSData from the server and round tripping it through a UIImage at all. You can theoretically just encode the data from the server directly:
NSURL* imageUrl = [NSURL URLWithString:url];
NSData* urlData = [NSData dataWithContentsOfURL:imageUrl];
return [urlData base64EncodedStringWithOptions:0];
The server is already returning you the NSData of a PNG representation. You don't need to do that UIImage and UIImagePNGRepresentation stuff at all. You're actually generating a PNG that is considerably larger than the one the server returned to you.
I'd advise against using dataWithContentsOfURL, because that's a synchronous network call. You probably should use NSURLSession and change this to be an asynchronous method.
NSURL* imageUrl = [NSURL URLWithString:#"https://api.qrserver.com/v1/create-qr-code/?data=somedata&size=220x220&margin=0"];
NSData *data = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = [UIImage imageWithData:data];
NSString *base64 = [self encodeToBase64String:image];
To convert your image to base64 String use following code:
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
That base64 will give you full image. Tested with your given image
Here is base 64 :
https://gist.github.com/anonymous/6d76e3ad852b4879ab097e6a1b3e68a2
To convert your UIImage into base64 string you can use this code.
NSString *base64String = [UIImagePNGRepresentation(uiImage) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
Here is the code.
NSURL* imageUrl = [NSURL URLWithString:#"https://api.qrserver.com/v1/create-qr-code/?data=somedata&size=220x220&margin=0"];
NSData* urlData = [NSData dataWithContentsOfURL:imageUrl];
UIImage* uiImage = [UIImage imageWithData: urlData];
NSString *base64String = [UIImagePNGRepresentation(uiImage) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSLog(#"%#",base64String);
I checked the result string on http://codebeautify.org/base64-to-image-converter
Try it, Hope it helps.
This is working perfectly
UIImage *img = [UIImage imageNamed:#"QRcode.png"];
NSString *base64 = [UIImagePNGRepresentation(img)base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSLog(base64);
The printed base64 String can be converted back to image in the URL you provided
Is there anyway to decrypt a NSString using RNcryptor that has been converted from NSData? I keep getting the unknown header error. Here is what I am doing to encrypt:
NSData *data = [#"FeedMeMorePizzaPlease" dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [RNEncryptor encryptData:data
withSettings:kRNCryptorAES256Settings
password:#"pizzaHutIsTheWorst"
error:&error];
NSString *encString = [encryptedData base64EncodedStringWithOptions:0];
I then take encString, put it in a text file on my server and then:
NSURL *gUrl = [NSURL URLWithString:#"https://myurlissupersecret.com/cheese.txt"];
NSString *sillyString = [NSString stringWithContentsOfURL:gUrl encoding:(NSUTF8StringEncoding) error:nil];
NSData *blindData = [sillyString dataUsingEncoding:0];
NSData *decryptedData = [RNDecryptor decryptData:blindData withSettings:kRNCryptorAES256Settings password:#"pizzaHutIsTheWorst" error:&error];
Is there anyway to do this? Or do I have to always encrypt / decrypt a file?
You missed a step in the decryption. sillyString is the base64 encoded string. You need to convert the base64 encoded string into NSData. Here is the updated code you need to decrypt:
NSURL *gUrl = [NSURL URLWithString:#"https://myurlissupersecret.com/cheese.txt"];
NSString *encString = [NSString stringWithContentsOfURL:gUrl encoding:NSUTF8StringEncoding error:nil];
NSData *encryptedData = [[NSData alloc] initWithBase64EncodedString:encString options:0];
NSData *decryptedData = [RNDecryptor decryptData:encryptedData withSettings:kRNCryptorAES256Settings password:#"pizzaHutIsTheWorst" error:&error];
I am using vector graphics in my application and want to store some on Parse.com. I need to first convert these to NSData before upload. Is it possible to convert a pdf file to NSData?
UIImage *image = [UIImage imageNamed:#"coolImage.pdf"];
NSData *data = UIImageJPEGRepresentation(image);
I know NSData allows for a jpg of png representation but what about pdfs? Any tips on this would be greatly appreciated!
//to convert pdf to NSData
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:#"test.pdf"];
NSData *myData = [NSData dataWithContentsOfFile:pdfPath];
I want to save multiple images to my documents path folder. My web service returns more than one image url path and i want to save each image to my documents folder.This process working successfully, but my view FREEZES at this time(converting image url to NSData).I am using following code.How to avoid this issue?
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[usersArray objectAtIndex:i]objectForKey:#"message"]]];
NSString *localImgpath = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.%#",imageName, #"png"]];
[data writeToFile:localImgpath atomically:YES];
You would want to process that in a background thread like this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[usersArray objectAtIndex:i]objectForKey:#"message"]]];
NSString *localImgpath = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.%#",imageName, #"png"]];
[data writeToFile:localImgpath atomically:YES];
});
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.