Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we convert the NSDecimal values to UIImage?

i am sending some data and UIImage(converting UIImage to NSData) sending to server, My client is giving back the response what we send.

But, Here my client giving the UIImage like the below format

FaceImage":[255,216,255,224,0,16,74,70,73,70,0,1,1,1,1,44,1,44,0,0,255,219,0,67,0,8,6,6,7,6,5,8,7,7,7,9,9,8,10,12,20,13,12,11,11,12,25,18,19,15,20,29,26,31,30,29,26,28,28,32,36,46,39,32,34,44,35,28,28,40,55,41,44,48,49,52,52,52,31,39,57,61,56,50,60,46,51,52,50,255,219,0,67,1,9,9,9,12,11,12,24,13,13,24,50,.................................................................]

i am tested the above one to test what we are getting by using this log

NSLog(@"testingImageView.image is =%@",[[[datadic valueForKey:@"FaceImage"] objectAtIndex:0] class]);

Response is: testingImageView.image is =NSDecimal.

If it is NSData we can convert to UIImage, But here it is in decimal format.

My question is, can i convert it to UIImage? i.e., the decimal values to UIImage.

Thanks in advance.

like image 265
Babul Avatar asked Feb 18 '23 03:02

Babul


1 Answers

Your response contains all the image bytes as decimal numbers. (255,216 is FF,D8 in hex, which indicates the start of a JPEG image). The following code should work to create an UIImage from the array of numbers:

NSArray *array = [datadic objectForKey:@"FaceImage"];
NSMutableData *imageData = [NSMutableData dataWithLength:[array count]];
uint8_t *imageBytes = [imageData mutableBytes];
for (NSUInteger i = 0; i < [array count]; i++) {
    imageBytes[i] = [array[i] unsignedCharValue];
}
UIImage *image = [UIImage imageWithData:imageData];
like image 113
Martin R Avatar answered Feb 27 '23 11:02

Martin R