nsdata

NSData to NSString with JSON response

爷,独闯天下 提交于 2019-12-17 16:22:17
问题 NSData* jsonData is the http response contains JSON data. NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"jsonString: %@", jsonString); I got the result: { "result": "\u8aaa" } What is the proper way to encoding the data to the correct string, not unicode string like "\uxxxx"? 回答1: If you convert the JSON data { "result" : "\u8aaa" } to a NSDictionary (e.g. using NSJSONSerialization ) and print the dictionary NSError *error; NSDictionary

Slow start for AVAudioPlayer the first time a sound is played

人走茶凉 提交于 2019-12-17 10:18:57
问题 I'm trying to eliminate startup lag when playing a (very short -- less than 2 seconds) audio file via AVAudioPlayer on the iPhone. First, the code: NSString *audioFile = [NSString stringWithFormat:@"%@/%@.caf", [[NSBundle mainBundle] resourcePath], @"audiofile"]; NSData *audioData = [NSData dataWithContentsOfMappedFile:audioFile]; NSError *err; AVAudioPlayer *audioPlayer = [(AVAudioPlayer*)[AVAudioPlayer alloc] initWithData:audioData error:&err]; audioPlayer.delegate = self; [audioPlayer play

Convert UIImage to NSData and convert back to UIImage in Swift?

邮差的信 提交于 2019-12-17 10:15:23
问题 I'm trying to save a UIImage to NSData and then read the NSData back to a new UIImage in Swift. To convert the UIImage to NSData I'm using the following code: let imageData: NSData = UIImagePNGRepresentation(myImage) How do I convert imageData (i.e., NSData ) back to a new UIImage ? 回答1: UIImage(data:imageData,scale:1.0) presuming the image's scale is 1. In swift 4.2, use below code for get Data(). image.pngData() 回答2: Thanks. Helped me a lot. Converted to Swift 3 and worked To save: let data

Byte Array to NSData

断了今生、忘了曾经 提交于 2019-12-17 07:27:30
问题 In a WebService JSON response is coming. In the response, there is image is coming as a byte array. I have to show the image in a UIImageView. I am trying to convert the byte array to NSData. But not getting how to do that. Any help would be appreciated. I am confident that the byte array has image data in it. Sample Byte array for your reference: (137, 80, 78, 71, ... 66, 96, 130) Thanks 回答1: You have to convert the JSON to an array of strings first; you can, for example, use the

NSData to UIImage

十年热恋 提交于 2019-12-17 06:46:22
问题 I'm trying to save a UIIMage and then retrieve it and display it. I've been successful by saving an image in the bundle, retrieving it from there and displaying. My problem comes from when I try to save an image to disk (converting it to NSData), then retrieving it. I convert the image to NSData like so... NSData* topImageData = UIImageJPEGRepresentation(topImage, 1.0); then I write it to disk like so... [topImageData writeToFile:topPathToFile atomically:NO]; then I tried to retrieve it like

NSData and Uploading Images via POST in iOS

妖精的绣舞 提交于 2019-12-17 04:14:22
问题 I have been combing through the many many posts about uploading images via POST in iOS. Despite the wealth of information on this topic, I cannot manage to correctly upload JPEG data taken from my iPhone Simulator Photo Library. The data, once on the server, is just a huge string of hexidecimal. Shouldn't NSData just be a byte stream? I don't get whats going on with all the hex, or why this code seems to work for everyone else. Here is the code in question: -(void)uploadWithUserLocationString

Converting NSData to base64

与世无争的帅哥 提交于 2019-12-17 03:37:38
问题 How to convert NSData to base64 . I have NSData and want to convert into base64 how can I do this? 回答1: EDIT As of OS X 10.9 / iOS 7, this is built into the frameworks. See -[NSData base64EncodedDataWithOptions:] Prior to iOS7/OS X 10.9: Matt Gallagher wrote an article on this very topic. At the bottom he gives a link to his embeddable code for iPhone. On the mac you can use the OpenSSL library, on the iPhone he writes his own impl. 回答2: //from: http://cocoadev.com/BaseSixtyFour + (NSString*

Converting NSData to base64

懵懂的女人 提交于 2019-12-17 03:37:07
问题 How to convert NSData to base64 . I have NSData and want to convert into base64 how can I do this? 回答1: EDIT As of OS X 10.9 / iOS 7, this is built into the frameworks. See -[NSData base64EncodedDataWithOptions:] Prior to iOS7/OS X 10.9: Matt Gallagher wrote an article on this very topic. At the bottom he gives a link to his embeddable code for iPhone. On the mac you can use the OpenSSL library, on the iPhone he writes his own impl. 回答2: //from: http://cocoadev.com/BaseSixtyFour + (NSString*

Reading and writing images to an SQLite DB for iPhone use

百般思念 提交于 2019-12-17 03:24:46
问题 I've set up a SQLite DB that currently reads and writes NSString s perfectly. I also want to store an image in the database and recall it later. I've read up a bit on using NSData and encoding the image, but I'm not entirely sure what the syntax is for what I want to do. Any code snippets or examples would be greatly appreciated. My current process goes like this: UIImagePickerController -> User Chooses Image from Photos -> chosenImage is set to instance of UIImageView -> Now I want to take

How do I convert an NSString value to NSData?

两盒软妹~` 提交于 2019-12-17 01:21:29
问题 How do I convert an NSString value to NSData ? 回答1: NSString* str = @"teststring"; NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding]; 回答2: NSString *str = @"helowrld"; // This converts the string to an NSData object NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; you can take reference from this link 回答3: Do: NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding]; then feel free to proceed with NSJSONSerialization:JSONObjectWithData . Correction to the