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.
nsdata
2,485
questions
950
votes
14
answers
465k
views
How do I convert an NSString value to NSData?
How do I convert an NSString value to NSData?
575
votes
7
answers
473k
views
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, ...
273
votes
24
answers
313k
views
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 ...
187
votes
31
answers
106k
views
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 *)...
181
votes
9
answers
167k
views
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 ...
177
votes
7
answers
147k
views
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 ...
177
votes
7
answers
178k
views
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 ...
172
votes
10
answers
218k
views
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 = ...
127
votes
6
answers
189k
views
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 = ...
112
votes
7
answers
69k
views
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 *...
103
votes
15
answers
56k
views
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 ...
90
votes
7
answers
88k
views
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 ...
88
votes
5
answers
78k
views
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 ...
82
votes
11
answers
65k
views
Print the size (megabytes) of Data in Swift
I have a variable fileData of Data type and I am struggling to find how to print the size of this.
In the past NSData you would print the length but unable to do that with this type.
How to print ...
77
votes
11
answers
50k
views
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 ...
76
votes
13
answers
99k
views
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(...
60
votes
5
answers
76k
views
NSData and UIImage
I am trying to load UIImage object from NSData, and the sample code was to NSImage, I guess they should be the same. But just now loading the image, I am wondering what's the best to troubleshoot the ...
59
votes
6
answers
90k
views
NSDictionary to NSData and NSData to NSDictionary in Swift
I am not sure if I am using the dictionary or the data object or both incorrectly. I m trying to get used to the switch to swift but I'm having a little trouble.
var dictionaryExample : [String:...
51
votes
7
answers
57k
views
How to convert String to byte in Swift?
How to convert String to byte in Swift?
Like String .getBytes()in Java.
46
votes
1
answer
31k
views
Is there a writeToFile equivalent for Swift 3's 'Data' type?
I've some code which returns the new iOS 10 / Swift 3 NSData replacement(?) type: Data
if let jpegData = UIImageJPEGRepresentation(newImage, 0.8) { ... }
I want to write this image to disk, however ...
44
votes
6
answers
74k
views
Converting between NSData and base64 strings
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 ...
44
votes
4
answers
29k
views
Converting HEX NSString To NSData
I'm trying to convert a Hex NSString to NSData (I'm using the below attached code). The following is the output:
<00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000>
which ...
43
votes
7
answers
62k
views
Converting NSData to base64
How to convert NSData to base64. I have NSData and want to convert into base64 how can I do this?
43
votes
3
answers
42k
views
NSData to display as a string
I am building an iPhone app and stuck with the following:
unsigned char hashedChars[32];
CC_SHA256([inputString UTF8String], [inputString lengthOfBytesUsingEncoding:NSASCIIStringEncoding], ...
43
votes
9
answers
24k
views
Slow start for AVAudioPlayer the first time a sound is played
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:@"...
41
votes
3
answers
53k
views
convert NSData Length from bytes to megs
I am trying to NSLog the number of megs my NSData object is however currently all I can get is bytes by using
NSLog(@"%u", myData.length);
So how would I change this NSLog statement so I can see ...
41
votes
3
answers
32k
views
NSData & NSURL - url with space having problem
I have following code in my application.
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
pathOfThumbNail has following path
http://70.84.58.40/projects/igolf/...
40
votes
2
answers
76k
views
Swift 3.0: Data to JSON [String : Any]
Evening, I'm trying to creating an APIClient, but I'm having a problem with a warning:
APIClient.swift:53:81: Cast from 'Data' to unrelated type '[String : Any]' always fails
In this code I'm trying ...
38
votes
7
answers
40k
views
How to convert an Int into NSData in Swift?
In Objective-C I use the following code to
Convert an Int variable into NSData, a packet of bytes.
int myScore = 0;
NSData *packet = [NSData dataWithBytes:&myScore length:sizeof(myScore)];
Use ...
38
votes
3
answers
42k
views
converting data back into a string
So I have this:
NSData *charlieSendData = [[charlieImputText stringValue] dataUsingEncoding:NSUTF8StringEncoding];
I know how to convert NSStrings to data but how I convert data back to an ...
37
votes
4
answers
47k
views
Create an Array in Swift from an NSData Object
I'm trying to store an array of integers to disk in swift. I can get them into an NSData object to store, but getting them back out into an array is difficult. I can get a raw COpaquePointer to the ...
37
votes
5
answers
61k
views
NSData and Uploading Images via POST in iOS
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 ...
36
votes
4
answers
37k
views
Determine MIME type from NSData?
How would you determine the mime type for an NSData object? I plan to have the user to upload a video/picture from their iPhone and have that file be wrapped in a NSData class.
I was wondering if I ...
34
votes
1
answer
48k
views
Convert unsigned char array to NSData and back
How do you convert an unsigned char array to an NSData in objective c?
This is what I am trying to do, but it doesn't work.
Buffer is my unsigned char array.
NSData *data = [NSData dataWithBytes:...
34
votes
2
answers
8k
views
Can I create an NSURL that refers to in-memory NSData?
The docs for NSURL state that:
An NSURL object represents a URL that can potentially contain the
location of a resource on a remote server, the path of a local file on
disk, or even an ...
31
votes
4
answers
21k
views
NSMutableData remove bytes?
I can add bytes to a NSMutableData instance easily by using the appendData method, however I do not see any similar method for removing data? Am I overlooking something, or do I need to create a new ...
31
votes
7
answers
84k
views
Converting JSON to NSData, and NSData to JSON in Swift
I'm having problems converting a JSON element into NSData, and an NSData variable back into JSON in Swift.
Firstly, I'd like to extract the encryptedData element of the following JSON data:
{
"...
31
votes
3
answers
36k
views
Converting custom class object into NSData
I have a custom class that I want to save into NSUserDefaults. I am told that I need to convert the class object into data in order to save it to NSUserDefaults. I found a lot of discrete string or ...
29
votes
5
answers
29k
views
Swift structs to NSData and back
I have a struct containing a struct and an NSObject that I want to serialize into an NSData object:
struct Packet {
var name: String
var index: Int
var numberOfPackets: Int
var data: NSData
}
...
27
votes
3
answers
25k
views
Copy a part of NSData byte array to another NSData type
I have an original NSData type which contains let's say 100 bytes. I want to get 2 other NSData types. The first containing the first 20 bytes of the 100, and the second one containing the other 80.
...
27
votes
2
answers
18k
views
How do we clear out contents in NSMutableData
How do we clear an NSMutableData without using release and then re-alloc/init again to be used again?
I was looking at resetBytesInRange to be set at zero but I am unsure of this. Anyone can help?
27
votes
1
answer
14k
views
Split NSData objects into other NSData objects of a given size
I have an NSData object of approximately 1000kB in size. Now I want to transfer this via Bluetooth. It would be better if I have, let's say, 10 objects of 100kB. It comes to mind that I should use the ...
27
votes
2
answers
27k
views
Iphone - How to encrypt NSData with public key and decrypt with private key?
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 ...
24
votes
5
answers
13k
views
NSData won't accept valid base64 encoded string
I'm implementing JSON Web Token authentication on the iOS (7) cient-side. It's working nicely. My app rceives tokens, and can make authenticated calls to my server with them.
Now, I want my client ...
23
votes
5
answers
63k
views
NSData to UIImage
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 ...
23
votes
2
answers
13k
views
Convert AVAudioPCMBuffer to NSData and back
How to convert AVAudioPCMBuffer to NSData? If it should be done as
let data = NSData(bytes: buffer.floatChannelData, length: bufferLength)
then how to calculate bufferLength?
And how to convert ...
23
votes
3
answers
22k
views
Convert UIImage to NSData without using UIImagePngrepresentation or UIImageJpegRepresentation
I Need to convert UIImage to NSData but without using UIImagePngRepresentation or UIImageJpegRepresentation, for images from photolib i can use assetlib method as mentioned here Using ALAssetsLibrary ...
22
votes
5
answers
27k
views
Unrecognized selector sent to instance while archiving data (NSCoding)
-(void)transformObjects:(NSMutableArray*)array key:(NSString*)key
{
NSMutableArray* archiveArray = [[NSMutableArray alloc]initWithCapacity:array.count];
for (Furniture *furniture in array) {
...
22
votes
1
answer
8k
views
UIDocumentInteractionController "invalid scheme (null)"
I'm trying to preview a document with UIDocumentInteractionController.
The document is an .xls file which my app downloads from the web. I keep getting this error:
'UIDocumentInteractionController: ...
22
votes
6
answers
40k
views
Reading and writing images to an SQLite DB for iPhone use
I've set up a SQLite DB that currently reads and writes NSStrings 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 ...