Questions tagged [nsdata]

The NSData class is an Apple class for holding generic data. Often used when reading/writing from and to files, and the internet.

From the Apple Developer Library:

NSData and its mutable subclass NSMutableData provide data objects, object-oriented wrappers for byte buffers. Data objects let simple allocated buffers (that is, data with no embedded pointers) take on the behavior of Foundation objects.

NSData creates static data objects, and NSMutableData creates dynamic data objects. NSData and NSMutableData are typically used for data storage and are also useful in Distributed Objects applications, where data contained in data objects can be copied or moved between applications.

The size of the data is subject to a theoretical limit of about 8 ExaBytes (in practice, the limit should not be a factor).

2485 questions
950
votes
14 answers

How do I convert an NSString value to NSData?

How do I convert an NSString value to NSData?
anish
575
votes
7 answers

Convert UTF-8 encoded NSData to NSString

I have UTF-8 encoded NSData from windows server and I want to convert it to NSString for iPhone. Since data contains characters (like a degree symbol) which have different values on both platforms, how do I convert data to string?
Ashwini Shahapurkar
  • 6,586
  • 6
  • 26
  • 35
270
votes
24 answers

Convert between UIImage and Base64 string

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 =…
changey
  • 18,606
  • 9
  • 28
  • 34
187
votes
31 answers

How can I convert my device token (NSData) into an NSString?

I am implementing push notifications. I'd like to save my APNS Token as a String. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken { NSString *tokenString = [NSString…
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
181
votes
9 answers

Creating NSData from NSString in Swift

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!,…
Jackson Egan
  • 2,715
  • 4
  • 18
  • 26
177
votes
7 answers

convert UIImage to NSData

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…
Vipin
  • 4,718
  • 12
  • 54
  • 81
177
votes
7 answers

How to initialise a string from NSData in Swift

I have been trying to initialise a string from NSData in Swift. In the NSString Cocoa Documentation Apple is saying you have to use this: init(data data: NSData!, encoding encoding: UInt) However Apple did not include any example for usage or…
ahmed
  • 14,316
  • 30
  • 94
  • 127
169
votes
10 answers

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

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…
pete
  • 2,980
  • 4
  • 22
  • 34
127
votes
6 answers

Convert NSData to String?

I am storing a openssl private Key EVP_PKEY as nsdata. For this I am serializing into a byte stream using the code below unsigned char *buf, *p; int len; len = i2d_PrivateKey(pkey, NULL); buf = OPENSSL_malloc(len); p = buf; i2d_PrivateKey(pkey,…
Zach
  • 9,989
  • 19
  • 70
  • 107
111
votes
7 answers

Get underlying NSData from UIImage

I can create UIImage from NSData using [UIImage imageWithData:] or [UIImage initWithData:] methods. I wonder if I can get the NSData back from an existing UIImage? Something on the line of NSData *myData = [myImage getData];
eugene
  • 39,839
  • 68
  • 255
  • 489
103
votes
15 answers

Best way to serialize an NSData into a hexadeximal string

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…
sarfata
  • 4,625
  • 4
  • 28
  • 36
89
votes
7 answers

NSData to [Uint8] in Swift

I couldn't find a solution to this problem in Swift (all of them are Objective-C, and they deal with pointers which I don't think exist in Swift in the same form). Is there any way to convert a NSData object into an array of bytes in the form of…
Bowen Su
  • 1,219
  • 3
  • 13
  • 15
88
votes
5 answers

iPhone - NSData from local file's URL

I have a NSURL object which gives me the path of a local file (in documents folder). I want to populate an NSData object with the contents of this file. Tried using dataWithContentsOfURL: but this fails. I know the file exists because the iPhone SDK…
lostInTransit
  • 70,519
  • 61
  • 198
  • 274
77
votes
11 answers

Finding image type from NSData or UIImage

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…
pschang
  • 2,568
  • 3
  • 28
  • 23
76
votes
13 answers

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

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*…
joshholat
  • 3,371
  • 9
  • 39
  • 48
1
2 3
99 100