2013-03-18 43 views
4

我發送一些數據和UIImage(將UIImage轉換爲NSData)發送到服務器,我的客戶正在回覆我們發送的響應。我們可以將NSDecimal值轉換爲UIImage嗎?

但是,在這裏我的客戶給的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的十進制值。

在此先感謝。

回答

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

哇...這救了我們的時間 – Babul 2013-03-18 10:28:20

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