无法在 ios 中显示图像

Not able to show image in ios

我从 ios 中的服务器获取一个 json,其中 returns 值之一作为数据类型 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 ..?

添加此代码:

 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

试试这个。

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

首先需要将图片数据转换成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);

试试这个..希望对你有帮助..谢谢