在我的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对象,图片和文本的应用程序,但从未遇到过这个问题。
欢迎任何帮助。
答案 0 :(得分:2)
在后台任务中下载你的图像,在主线程中进行网络操作会导致真正的问题:
要在后台任务中下载图像,您可以执行以下操作:
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"];
}
});
});
}
希望这有助于......:)
答案 1 :(得分:1)
您应该异步下载图像并将其保存在对象中并重复使用。
- (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);
}
}];
}
答案 2 :(得分:1)
在这里,您正在从Internet上下载数据,并将其转换为图像,所有这些都在主线程上:
NSURL *logoURL = [NSURL URLWithString:logo];
NSData *logoData = [NSData dataWithContentsOfURL:logoURL];
cell.imagen_label.image = [UIImage imageWithData:logoData];
不要这样做。尽快返回您的牢房。异步下载和处理图像。 Stack Overflow上有很多关于各种方法的问题,甚至open source libraries也是为你设计的。
答案 3 :(得分:1)
尝试使用SDWebImage进行图片上传。