我们可以将NSDecimal值转换为UIImage吗?

时间:2013-03-18 09:39:13

标签: iphone ios objective-c image nsdecimalnumber

我发送一些数据和UIImage(将UIImage转换为NSData)发送到服务器,我的客户端正在回复我们发送的响应。

但是,我的客户端给出了UIImage,如下面的格式

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,.................................................................]

我测试了上面的一个来测试我们通过使用这个日志得到的东西

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

响应是: testingImageView.image = NSDecimal。

如果它是NSData,我们可以转换为UIImage,但这里是十进制格式。

我的问题是,我可以将其转换为UIImage吗?即UIImage的十进制值。

提前致谢。

2 个答案:

答案 0 :(得分:7)

您的回复包含所有图像字节作为十进制数字。 (255,216为十六进制FF,D8,表示JPEG图像的开头)。以下代码可用于从数组中创建UIImage

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

答案 1 :(得分:1)

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