在iOS中下载JSON对象时出现问题
在我的iOS应用程序中,我使用JSON上传MySQL对象 这是CelforRowatineXpath方法:在iOS中下载JSON对象时出现问题,ios,json,Ios,Json,在我的iOS应用程序中,我使用JSON上传MySQL对象 这是CelforRowatineXpath方法: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { Lista_Actividades_TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"C
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Lista_Actividades_TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell ==nil){
cell = [[Lista_Actividades_TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
cell.dia_label.hidden = NO;
cell.minutos_label.hidden = NO;
cell.hora_label.hidden = NO;
cell.mes_label.hidden = NO;
cell.puntos_label.hidden = NO;
NSString *hora = [[categorias objectAtIndex:indexPath.row] objectForKey:@"hora_evento"];
NSString *minutos = [[categorias objectAtIndex:indexPath.row] objectForKey:@"minutos_evento"];
if ([hora isEqualToString:@"0"]){
hora = @"00";
}
if ([minutos isEqualToString:@"0"]){
minutos = @"00";
}
if ([ [[categorias objectAtIndex:indexPath.row] objectForKey:@"hora_evento"] isEqualToString:@"0"] && [[[categorias objectAtIndex:indexPath.row] objectForKey:@"minutos_evento"] isEqualToString:@"0"]){
cell.hora_label.hidden = YES;
cell.puntos_label.hidden = YES;
}
else
{
cell.hora_label.text = [[categorias objectAtIndex:indexPath.row] objectForKey:@"hora_evento"];
}
if ([[[categorias objectAtIndex:indexPath.row] objectForKey:@"hora_evento"] isEqualToString:@"0"] && [[[categorias objectAtIndex:indexPath.row] objectForKey:@"minutos_evento"] isEqualToString:@"0"]){
cell.minutos_label.hidden = YES;
}
else
{
cell.minutos_label.text = minutos;
}
if ([ [[categorias objectAtIndex:indexPath.row] objectForKey:@"dia_evento"] isEqualToString:@"0"]){
cell.dia_label.hidden = YES;
}
else
{
cell.dia_label.text = [[categorias objectAtIndex:indexPath.row] objectForKey:@"dia_evento"];
}
NSString *mes_evento = [[categorias objectAtIndex:indexPath.row] objectForKey:@"mes_evento"];
int value = [mes_evento intValue];
if ([ [[categorias objectAtIndex:indexPath.row] objectForKey:@"mes_evento"] isEqualToString:@"0"]){
cell.mes_label.hidden = YES;
}
else
{
switch (value)
{
case 1:
cell.mes_label.text =@"ENERO";
break;
case 2:
cell.mes_label.text =@"FEBRERO";
break;
case 3:
cell.mes_label.text =@"MARZO";
break;
case 4:
cell.mes_label.text =@"ABRIL";
break;
case 5:
cell.mes_label.text =@"MAYO";
break;
case 6:
cell.mes_label.text =@"JUNIO";
break;
case 7:
cell.mes_label.text =@"JULIO";
break;
case 8:
cell.mes_label.text =@"AGOSTO";
break;
case 9:
cell.mes_label.text =@"SEPTIEMBRE";
break;
case 10:
cell.mes_label.text =@"OCTUBRE";
break;
case 11:
cell.mes_label.text =@"NOVIEMBRE";
break;
case 12:
cell.mes_label.text =@"DICIEMBRE";
break;
default:
cell.mes_label.text =@"SIN MES";
break;
}
}
cell.titulo_label.text = [[[categorias objectAtIndex:indexPath.row] objectForKey:@"titulo_evento"] uppercaseString];
NSString *lugar = [[categorias objectAtIndex:indexPath.row] objectForKey:@"lugar_evento"];
if ([lugar isEqualToString:@"0"]){
NSLog(@"LUGAR AQUI ES=%@",lugar);
cell.lugar_label.text = @" ";
}
else {
cell.lugar_label.text = [[categorias objectAtIndex:indexPath.row] objectForKey:@"lugar_evento"];
}
NSMutableString *logo = [[NSMutableString alloc]initWithString:@"hidden here"];
NSString *imageURL = [[categorias objectAtIndex:indexPath.row] objectForKey:@"imagen_evento"];
if(imageURL != nil && ![imageURL isEqual:[NSNull null]])
{
[logo appendString:imageURL];
NSURL *logoURL = [NSURL URLWithString:logo];
NSData *logoData = [NSData dataWithContentsOfURL:logoURL];
cell.imagen_label.image = [UIImage imageWithData:logoData];
}
else{
cell.imagen_label.image = [UIImage imageNamed:@"no_image-512.png"];
}
return cell;
}
共有45个对象,并且始终在同一个对象上,应用程序崩溃,出现以下错误:
URL = ACTteatro.jpg
mujergrancanaria(4031,0x4f9e1d4) malloc: *** mach_vm_map(size=8388608) failed (error code=3)
*** error: can't allocate region securely
*** set a breakpoint in malloc_error_break to debug
mujergrancanaria(4031,0x4f9e1d4) malloc: *** mach_vm_map(size=8388608) failed (error code=3)
*** error: can't allocate region securely
*** set a breakpoint in malloc_error_break to debug
libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
我开发的应用程序可以下载更多的JSON对象、图片和文本,但从未遇到过这个问题
欢迎任何帮助。您应该异步下载图像,并将其保存在object中,然后重新使用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"venue";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
Venue *venue = ((Venue * )self.venues[indexPath.row]);
if (venue.userImage) {
cell.imageView.image = venue.image;
} else {
// set default user image while image is being downloaded
cell.imageView.image = [UIImage imageNamed:@"batman.png"];
// download the image asynchronously
[self downloadImageWithURL:venue.url completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
// change the image in the cell
cell.imageView.image = image;
// cache the image for use later (when scrolling up)
venue.image = image;
}
}];
}
}
- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ( !error )
{
UIImage *image = [[UIImage alloc] initWithData:data];
completionBlock(YES,image);
} else{
completionBlock(NO,nil);
}
}];
}
在这里,您正在从Internet下载数据,并将其转换为图像,所有这些都在主线程上:
NSURL *logoURL = [NSURL URLWithString:logo];
NSData *logoData = [NSData dataWithContentsOfURL:logoURL];
cell.imagen_label.image = [UIImage imageWithData:logoData];
不要那样做。尽快归还手机。下载并异步处理映像。有很多关于堆栈溢出的问题,展示了各种方法,甚至是专门为您设计的。在后台任务中下载您的图像,在主线程中执行网络操作会导致严重的问题: 要在后台任务中下载图像,可以执行以下操作:
if(imageURL != nil && ![imageURL isEqual:[NSNull null]])
{
[logo appendString:imageURL];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURL *logoURL = [NSURL URLWithString:logo];
NSData *logoData = [NSData dataWithContentsOfURL:logoURL];
dispatch_async(dispatch_get_main_queue(), ^{
if (logoData.length) {
cell.imagen_label.image = [UIImage imageWithData:logoData];
}else{
cell.imagen_label.image = [UIImage imageNamed:@"no_image-512.png"];
}
});
});
}
希望这有帮助……:) 尝试使用SDWebImages进行图像上载 谢谢你,我知道,它只是一个知道图像名称的标签…它在NSLog中。好的,你可以在这里共享整个url。url不是问题,直到应用程序崩溃的对象,所有图片都显示出来。我已经删除了行:NSLog(@“url=%@”,imageURL);问题仍然存在。您是否保存下载的图像并在重新使用cell时再次使用它们?谢谢,我也会尝试Rashad的答案。您是否介意向我解释正确的方法。我的意思是,对于每个对象调用CellForRowatinex方法,这意味着数据放在单元格中,图像从internet下载并放在图像视图容器中,对吗?但是我已经完成了我的代码,负载太高,应用程序崩溃。谢谢你,我还会尝试Rashad的答案。谢谢你Rashasd,实施你的建议会让图像下载更快,但在同一个对象上,应用程序没有崩溃,但停止工作,它冻结。它最终崩溃了…:(与之前相同:mujergrancanaria(4757,0x4fde1d4)malloc:**mach_vm_映射(大小=8388608)失败(错误代码=3)***错误:无法分配区域securely@ModestoVasco>我很确定你的应用程序有内存泄漏。你能监控你使用了多少内存吗?在崩溃瞬间,5.7 Mb