NSData to byte array (or array of integers)

Freelance Jobs

I am working with a webservice which gives me an image as an array of integers. I have managed to convert this array into an NSData object and then into a UIImage using the code below. Now I am in doubt how I will convert an image into a similar array.

The array looks like this:


This is the code I use to convert the above array to a UIImage.

+ (UIImage *)imageFromBytes:(NSArray *)bytes
    unsigned char *buffer = (unsigned char *) malloc(bytes.count);
    int i = 0;
    for (NSDecimalNumber *num in bytes)
        buffer[i++] = num.intValue;
    NSData *data = [NSData dataWithBytes:buffer length:bytes.count];

    return [UIImage imageWithData:data];

So, I need to convert a UIImage into an array of integers (a byte array).

Can anybody point me in the right direction?

2012-05-01 19:06:46 Scores:1

1 answer

Answer 1

You can get the data for an image with:

UIImage *image = [UIImage imageNamed:@"image.png"];
NSData *data = UIImagePNGRepresentation(image);

If you use a JPG you can use UIImageJPEGRepresentation.

And then, extract the array of bytes:

NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);

You can use also the method (void)getBytes:(void *)buffer length:(NSUInteger)length from NSData:

[data getBytes:&byteData length:len];
Jonathan Naguin H
2012-05-01 19:25:20
Share |
View original post at stackoverflow.com

Related topics

I used to use them all the time as Daok says for events, until I learned that you can use built-in handlers for most common events (common for what I do, at least). Now I usually use them as callback handlers for API interaction. ...

How to convert NSData to byte array in iPhone?

I want to convert NSData to a byte array, so I write the following code: NSData *data = [NSData dataWithContentsOfFile:filePath]; int len = [data length]; Byte byteData[len]; byteData = [data bytes]; But the last line of code pops up an error saying "incompatible types in assignment". What is the correct way to convert the data to ...

2d Byte array

Hii all, m trying to make a 2d byte array . can any body give the code how to declare a NULL 2d byte array in Obj C for iPhone? Thanks all. ...

iOS Development: Can I store an array of integers in a Core Data object without creating a new table to represent the array?

I'm using Core Data and I'm trying to figure out the simplest way to store an array of integers in one of my Core Data entities. Currently, my entities contain various arrays of objects that are more complex than a single number, so it makes sense to represent those ...

removing integers from an array

I need to make an array for the co ordinates of my sprites and i wanted to remove the co-ordinate once the sprite has that co-ordinate from the array as i don't want another sprite having the same co-ordinate, i can't seem to get it to work, there are no ...

Assigning a button tag from an array which stores integers

I am trying to assign a tag to button. The normal command is: button.tag = 1; The tag must be an integer. My problem is that I would like to assign an integer which I stored in an array (tabReference) which is yet again part of a class (currentNoteBook). So I need this: int ...

Unable to sort an Array of integers?

i want to sort an array of integers with multiple digits in ascending order. Here is my Array: keyArray: ( 978, 1077, 1067, 1076, 1072, 1082, 1079, 1075, 1071, 1081, 1078, ...

RestKit and Core Data array of Integers

I have an array of integers in my core data model. It is set up as a Transformable. The JSON I get back from the server gives me this list of numbers in the following format: { "numbers": [10, 3, 2], "name": "John Smith" } I configure my object mapping as follows: [dataMapping mapKeyPath:@"numbers" ...

Sort array by integers?

i have an array that named detailTexts that having his data from floats: CLLocation *location = [[CLLocation alloc] initWithLatitude:dlog longitude:dlig]; **CLLocationDistance meters = [userLocation distanceFromLocation:location];** **float detailFloat = meters * 1000;** if (meters < 10000) { NSLog(@"%@", detailText); [tableData addObject:name]; **detailText = [NSString stringWithFormat:@"%f", detailFloat];** ...

Ios create pdf from array of byte

i want create a pdf from a stream of byte (that i receive from a wsdl), i have insert this stream of byte on NSArray but i don't know how write the pdf. (This stream of byte is a pdf) Thanks! ...