gpt4 book ai didi

ios - 在 iOS 中下载 JSON 对象时出现问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:26:48 25 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我使用 JSON 上传 MySQL 对象。

这是 celForRowAtIndexPath 方法:

- (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 对象、图片和文本的应用程序,但从未遇到过此问题。

欢迎任何帮助。

最佳答案

在后台任务中下载你的图片,在主线程中进行网络操作会导致严重的问题:

要在后台任务中下载图片,您可以这样做:

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"];
}
});
});
}

希望这有助于...:)

关于ios - 在 iOS 中下载 JSON 对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27700105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com