nsdata

Converting between NSData and base64 strings

牧云@^-^@ 提交于 2019-11-26 09:26:52
问题 What is the easiest and fastest code to do a conversion between NSData and a base64 string? I\'ve read a bunch of solutions at SO and mostly they involve in adding another class etc. I found a great solution here but it\'s too complex. 回答1: Scroll down to the Conclusion section on the page you linked and download the provided NSData+Base64 files. Its the best solution I have seen so far and is incredibly easy to use. If you can learn anything about Cocoa, you can learn to use that project.

How do I convert an NSString value to NSData?

≯℡__Kan透↙ 提交于 2019-11-26 09:09:22
How do I convert an NSString value to NSData ? flitzwald NSString* str = @"teststring"; NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding]; ALOK KUMAR NSString *str = @"helowrld"; // This converts the string to an NSData object NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; you can take reference from this link Andrew Kolesnikov Do: NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding]; then feel free to proceed with NSJSONSerialization:JSONObjectWithData . Correction to the answer regarding the NULL terminator Following the comments, official documentation,

Iphone - How to encrypt NSData with public key and decrypt with private key?

大憨熊 提交于 2019-11-26 08:48:22
问题 I am converting a UIImage to NSData. Now I need to encrypt that NSData using a public key and I need to decrypt using a private key. Please provide a step by step procedure. Which algorithm do I need to use? Is there any good library for encryption and decryption? Also provide some code snippet for encryption and decryption. 回答1: I have tried RSA Encryption and Decryption for NSString and you may well modify it and make it work for NSData Add Security.Framework to your project bundle.

Creating NSData from NSString in Swift

时光怂恿深爱的人放手 提交于 2019-11-26 07:07:08
问题 I\'m trying to ultimately have an NSMutableURLRequest with a valid HTTPBody , but I can\'t seem to get my string data (coming from a UITextField ) into a usable NSData object. I\'ve seen this method for going the other way: NSString(data data: NSData!, encoding encoding: UInt) But I can\'t seem to find any documentation for my use case. I\'m open to putting the string into some other type if necessary, but none of the initialization options for NSData using Swift seem to be what I\'m looking

convert UIImage to NSData

亡梦爱人 提交于 2019-11-26 04:35:47
问题 I am using this code in my app which will help me to send a image. However, I have an image view with an image. I have no file in appbundle but have the image in my side. How can I change the below code ? Can anyone tell me how can I convert myimage to NSData ? // Attach an image to the email NSString *path = [[NSBundle mainBundle] pathForResource:@\"rainy\" ofType:@\"jpg\"]; NSData *myData = [NSData dataWithContentsOfFile:path]; [picker addAttachmentData:myData mimeType:@\"image/jpeg\"

Finding image type from NSData or UIImage

放肆的年华 提交于 2019-11-26 04:32:53
问题 I am loading an image from a URL provided by a third-party. There is no file extension (or filename for that matter) on the URL (as it is an obscured URL). I can take the data from this (in the form of NSData) and load it into a UIImage and display it fine. I want to persist this data to a file. However, I don\'t know what format the data is in (PNG, JPG, BMP)? I assume it is JPG (since it\'s an image from the web) but is there a programmatic way of finding out for sure? I\'ve looked around

How to compress/resize image on iOS before uploading to a server?

♀尐吖头ヾ 提交于 2019-11-26 03:05:37
问题 I\'m currently uploading an image to a server using Imgur on iOS with the following code: NSData* imageData = UIImagePNGRepresentation(image); NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* fullPathToFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@\"SBTempImage.png\"]; [imageData writeToFile:fullPathToFile atomically:NO]; [uploadRequest setFile:fullPathToFile forKey:@\"image\"]; The code works fine when run in the

Best way to serialize an NSData into a hexadeximal string

二次信任 提交于 2019-11-26 01:48:59
问题 I am looking for a nice-cocoa way to serialize an NSData object into a hexadecimal string. The idea is to serialize the deviceToken used for notification before sending it to my server. I have the following implementation, but I am thinking there must be some shorter and nicer way to do it. + (NSString*) serializeDeviceToken:(NSData*) deviceToken { NSMutableString *str = [NSMutableString stringWithCapacity:64]; int length = [deviceToken length]; char *bytes = malloc(sizeof(char) * length);

Best way to serialize an NSData into a hexadeximal string

删除回忆录丶 提交于 2019-11-26 00:33:41
I am looking for a nice-cocoa way to serialize an NSData object into a hexadecimal string. The idea is to serialize the deviceToken used for notification before sending it to my server. I have the following implementation, but I am thinking there must be some shorter and nicer way to do it. + (NSString*) serializeDeviceToken:(NSData*) deviceToken { NSMutableString *str = [NSMutableString stringWithCapacity:64]; int length = [deviceToken length]; char *bytes = malloc(sizeof(char) * length); [deviceToken getBytes:bytes length:length]; for (int i = 0; i < length; i++) { [str appendFormat:@"%02

Convert between UIImage and Base64 string

霸气de小男生 提交于 2019-11-25 22:00:19
问题 Does anyone know how to convert a UIImage to a Base64 string, and then reverse it? I have the below code; the original image before encoding is good, but I only get a blank image after I encode and decode it. NSData *imageData = UIImagePNGRepresentation(viewImage); NSString *b64EncStr = [self encode: imageData]; NSString *base64String = [self encodeBase64:imageData]; 回答1: Swift First we need to have image's NSData //Use image name from bundle to create NSData let image : UIImage = UIImage