无法在ios中显示图像

时间:2015-04-16 06:59:57

标签: ios objective-c uiimageview

我从ios中的服务器获取一个json,它返回一个值作为数据类型byte [],我收到像

  NSData *imageData =[receivedData objectForKey:@"img1"];

如果我使用NSLog在iOS中打印,则会显示如下内容:

(
    "-1",
    "-40",
    "-1",
    "-32",
    0,
    16,
    74,
    70,
    73,
    70,
    0,
    1,
    2,
    1,
    0,
    72,
    0,
    72,
    0,
    0,
    "-1",
    "-31",
    20,
    "-79",
    69,
    120,
    105,
    102,
    0,
    0,
    77,
    77,
    0,
    42,
    0,
    0,
    0,
    8,
    0,
    7,
    1,
    18,
    0,
    3,
    0,
    0,
    0,
    1,
    0,
    1,
    0,
    0,
    1,
    26,
    0,
    5,
    0,
    0,
    0,
    1,
    0,
    0,
    0,
    98,
    1,
    27,
    0,
    5,
   ....)

正如显示一些双引号显示的值是问题?由于这在web应用程序中运行良好,只需要在iOS中将此数组显示为图像吗?我必须使用来自服务器的字节数组在iOS中显示图像。

任何人都可以指出我是iOS新手的问题吗?

3 个答案:

答案 0 :(得分:1)

添加此代码:

 NSArray *array = [receivedData objectForKey:@"img1"];
unsigned c = array.count;
uint8_t *bytes = malloc(sizeof(*bytes) * c);

unsigned i;
for (i = 0; i < c; i++)
{
    NSString *str = [array objectAtIndex:i];
    int byte = [str intValue];
    bytes[i] = byte;
}
NSData *imageData =[NSData dataWithBytesNoCopy:bytes length:c freeWhenDone:YES];// your imageData    
UIImage *img=[UIImage imageWithData:data]; // your image
[yourImageView setImage:img];// set to imageView

答案 1 :(得分:1)

试试这个。

  NSArray *byteArrayReceived =  [receivedData objectForKey:@"img1"];
  unsigned c = byteArrayReceived.count;
  uint8_t *bytes = malloc(sizeof(*bytes) * c);

    unsigned i;
    for (i = 0; i < c; i++)
    {
        NSString *str = [byteArrayReceived objectAtIndex:i];
        int byte = [str intValue];
        bytes[i] = (uint8_t)byte;
    }

NSData *data = [NSData dataWithBytes:bytes length:c];
UIImage *image = [UIImage imageWithData:data];

答案 2 :(得分:0)

首先,您需要将图像数据转换为Base64String。

创建一个NSData类别并编写用于将字符串转换为base 64

的代码
#define BINARY_UNIT_SIZE 3
#define BASE64_UNIT_SIZE 4

void *Base64Decode(
                  const char *inputBuffer,
                  size_t length,
                  size_t *outputLength)
{
    if (length == -1)
    {
        length = strlen(inputBuffer);
    }

    size_t outputBufferSize = ((length+BASE64_UNIT_SIZE-1) / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE;unsigned char *outputBuffer = (unsigned char*)malloc(outputBufferSize);

   size_t i = 0;
   size_t j = 0;
   while (i < length)
   {
        unsigned char accumulated[BASE64_UNIT_SIZE];
        size_t accumulateIndex = 0;
        while (i < length)
        {
            unsigned char decode = base64DecodeLookup[inputBuffer[i++]];
            if (decode != xx)
            {
                accumulated[accumulateIndex] = decode;
                accumulateIndex++;

                if (accumulateIndex == BASE64_UNIT_SIZE)
                {
                  break;
                }
           }
       }

      if(accumulateIndex >= 2)  
        outputBuffer[j] = (accumulated[0] << 2) | (accumulated[1] >> 4);  
      if(accumulateIndex >= 3)  
        outputBuffer[j + 1] = (accumulated[1] << 4) | (accumulated[2] >> 2);  
      if(accumulateIndex >= 4)  
        outputBuffer[j + 2] = (accumulated[2] << 6) | accumulated[3];
        j += accumulateIndex - 1;
   }

  if (outputLength)
  {
      *outputLength = j;
  }
  return outputBuffer;
}

// Convert string to base64String
+ (NSData *)dataFromBase64String:(NSString *)aString 
{
    NSData *data = [aString dataUsingEncoding:NSASCIIStringEncoding];
    size_t outputLength;
    void *outputBuffer = Base64Decode([data bytes], [data length],     &outputLength);
    NSData *result = [NSData dataWithBytes:outputBuffer length:outputLength];
    free(outputBuffer);
    return result;
}

// now write this code and convert nsdata to image
NSData* data = [NSData dataFromBase64String:[receivedData objectForKey:@"img1"];];
UIImage* image = [UIImage imageWithData:data];
NSLog(@"Image --- %@", image);

试试这个..希望它可以帮助你..谢谢