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.

有帮助吗?

解决方案

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];

其他提示

NSDecimalNumber *myNSDecimalNumber = [NSDecimalNumber decimalNumberWithDecimal:myNSDecimal]; 
NSInteger myInt = [myNSDecimalNumber intValue];// convert nsdeimal to nsdecimal number and then to nsinteger

NSUInteger index = (NSUInteger)myInt;
NSData *payload = [NSData dataWithBytes:&index length:sizeof(index)];// convert nsinteger to nsdata

UIImageView *imgv = [[UIImageView alloc]
                     initWithFrame:CGRectMake(100, 100, 200, 200)];
imgv.image = [UIImage imageWithData:retrievedData];// converting into image th nsdata
[self.view addSubview:imgv];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top