我试图从JSON获取Image。 Imagedata正在控制台中打印。但它在UITableViewCell中显示。
jsonDict = [jsonArr objectAtIndex:indexPath.row];
的NSLog(@" imagess%@",jsonDict);
cell.textLabel.text = [jsonDict objectForKey:@"ImageName"];
cell.textLabel.textAlignment=NSTextAlignmentRight;
NSURL *url = [NSURL URLWithString:[jsonDict objectForKey:@"ImageData"]];
NSData *imgData = [NSData dataWithContentsOfURL:url];
cell.imageView.image = [UIImage imageWithData:imgData];
我用于发布图片的代码:
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);
[request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];
答案 0 :(得分:2)
新答案
根据您的数据日志,您似乎已在imageData
键中获得了整个图像。这看起来好像是base64 encoded
。
您不需要使用URL
,您需要base64解码器,它会将您的base64编码字符串转换为NSData
。
请与api团队确认,是否编码为Base64
或不是。
从here
获取适用于iOS的Base64 lib您只需要执行以下操作
NSData *imgData = [NSData dataWithBase64EncodedString:[jsonDict objectForKey:@"ImageData"]];
cell.imageView.image = [UIImage imageWithData:imgData];
干杯。
答案 1 :(得分:2)
尝试此代码可能会对您有所帮助
此处 imageurl 是您将为JSON和图片网址访问的图片的地址,例如http:/google/123.png,在此示例中单元格是UITableView在我的应用中使用的单元格。
NSURL* url = [NSURL URLWithString:imageurl];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) {
if (!error){
cell.imagePic.image = [UIImage imageWithData: data];
}
}];
由于