Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte Array to NSData

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

like image 404
Subhendu Kumar Mukherjee Avatar asked Aug 08 '12 08:08

Subhendu Kumar Mukherjee


People also ask

What is NSData in iOS?

A static byte buffer that bridges to Data ; use NSData when you need reference semantics or other Foundation-specific behavior. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+

What is NSData object?

An NSData object is simply a container of bytes (raw binary data in the form of 1's and 0's).

What is byte array in Swift?

In Swift a byte is called a UInt8—an unsigned 8 bit integer. A byte array is a UInt8 array. In ASCII we can treat chars as UInt8 values. With the utf8 String property, we get a UTF8View collection. We can convert this to a byte array.

What is NSMutableData?

NSMutableData and its superclass NSData provide data objects, or 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.


1 Answers

Here is an example of getting your data into a NSData object.

const unsigned char bytes[] = {values here};
NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
NSLog(@"%@", data);
like image 134
Paresh Navadiya Avatar answered Oct 21 '22 19:10

Paresh Navadiya