无法在ios中显示图像
我从ios中的服务器获得一个json,它返回一个值作为数据类型byte[],我收到的结果如下无法在ios中显示图像,ios,objective-c,uiimageview,Ios,Objective C,Uiimageview,我从ios中的服务器获得一个json,它返回一个值作为数据类型byte[],我收到的结果如下 NSData *imageData =[receivedData objectForKey:@"img1"]; 如果我在iOS中使用NSLog打印它,它会显示如下内容: ( "-1", "-40", "-1", "-32", 0, 16, 74, 70, 73, 70, 0, 1, 2,
NSData *imageData =[receivedData objectForKey:@"img1"];
如果我在iOS中使用NSLog
打印它,它会显示如下内容:
(
"-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*array=[receiveddataobjectforkey:@“img1”];
无符号c=array.count;
uint8_t*字节=malloc(sizeof(*字节)*c);
未签名的i;
对于(i=0;i
试试这个
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];
NSArray*byteArrayReceived=[receivedData objectForKey:@“img1”];
无符号c=byteArrayReceived.count;
uint8_t*字节=malloc(sizeof(*字节)*c);
未签名的i;
对于(i=0;i
首先需要将图像数据转换为Base64String
对NSData进行分类,并编写将字符串转换为基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);
#定义二进制单位大小3
#定义BASE64\u单位\u大小4
void*base64解码(
常量字符*输入缓冲区,
大小和长度,
大小(t*输出长度)
{
如果(长度==-1)
{
长度=strlen(输入缓冲区);
}
大小\u t outputBufferSize=((长度+BASE64\u单位\u大小-1)/BASE64\u单位\u大小)*二进制\u单位\u大小;无符号字符*outputBuffer=(无符号字符*)malloc(outputBufferSize);
尺寸i=0;
尺寸j=0;
while(i=2)
outputBuffer[j]=(累计[0]>4);
如果(累计指数>=3)
outputBuffer[j+1]=(累计[1]>2);
如果(累计指数>=4)
outputBuffer[j+2]=(累计[2]感谢您的快速回复,我尝试了这个方法,如果我这样做,我会得到以下异常:u nsFarray length]:未识别的选择器发送到实例0x11b731000***由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因:'-[uu nsFarray length]:无法识别的选择器发送到实例0x11b731000'您能否告诉我NSLog(@“%@”,imageData)
和NSLog(@“%@”,receivedData)
第NSString*str=[strings objectAtIndex:i]行中的字符串是什么;