Cocoa touch 删除子视图时的CALayer僵尸
我有一个详细的视图。其中一个值是指向图像的URL 如果URL不是nil,它将创建一个临时视图以显示进度条,并在后台下载带有Cocoa touch 删除子视图时的CALayer僵尸,cocoa-touch,view,Cocoa Touch,View,我有一个详细的视图。其中一个值是指向图像的URL 如果URL不是nil,它将创建一个临时视图以显示进度条,并在后台下载带有NSURLConnection的图像。准备好后,我删除进度视图,创建并添加一个新的图像视图,用foto替换旧的进度视图 当我尝试使用[[self.view viewWithTag:1100]removeFromSuperview]删除视图时,我总是获得EXC\u BAD\u访问权限。我已经检查了每一行代码,无法猜测错误在哪里,所以可能我遗漏了一些规则 仪器为我提供了以下信息:
NSURLConnection
的图像。准备好后,我删除进度视图,创建并添加一个新的图像视图,用foto替换旧的进度视图
当我尝试使用[[self.view viewWithTag:1100]removeFromSuperview]删除视图时,我总是获得EXC\u BAD\u访问权限代码>。我已经检查了每一行代码,无法猜测错误在哪里,所以可能我遗漏了一些规则
仪器为我提供了以下信息:
# Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller
0 0x5ded960 CALayer Malloc 1 00:10.772.601 48 UIKit -[UIView _createLayerWithFrame:]
1 0x5ded960 CALayer Zombie -1 00:14.720.076 0 QuartzCore -[CALayerArray copyWithZone:]
#地址类别事件类型RefCt时间戳大小责任库责任调用方
0 0x5ded960 CALayer Malloc 1 00:10.772.601 48 UIKit-[UIView\u createLayerWithFrame:]
1 0x5ded960 CALayer僵尸-1 00:14.720.076 0夸脱核心-[CALayer阵列copyWithZone:]
这是代码:
- (void) loadDataIntoView
{
// Create the View without the photo
self.nombreTextView.text = self.evento.nombre;
self.fechaTextField.text = @"Del 12 de Junio de 2010 al 3 de Octubre de 2011";
// Check is a photo url exists
if (self.evento.foto) {
// Download the photo in background
self.fotoConnection = [NSURLConnection connectionWithRequest:
[NSURLRequest requestWithURL:self.evento.foto]
delegate:self];
// Erros are handled by the delegate methods
// This error should never happen
if (!self.fotoConnection) {
NSLog(@"Error creating connection");
exit(0);
}
// Create the entradilla View
UITextView *entradillaTextView = [[UITextView alloc] init];
entradillaTextView.frame = CGRectMake(26, 216, 280, 145);
entradillaTextView.text = self.evento.entradilla;
entradillaTextView.tag = 1000;
[self.view addSubview:entradillaTextView];
[entradillaTextView release];
// Create the progess View
UIView *progressView = [[UIView alloc] initWithFrame:CGRectMake(26, 76, 270, 130)];
progressView.tag = 1100;
// Create progressView Label
UILabel *legendLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 270, 20)];
legendLabel.text = @"descargando fotografía";
legendLabel.font = [UIFont systemFontOfSize:11];
legendLabel.textColor = [UIColor lightGrayColor];
legendLabel.textAlignment = UITextAlignmentCenter;
// Create progressView progressBar
UIProgressView *auxView = [[UIProgressView alloc] initWithFrame:CGRectMake(35, 20, 200, 40)];
self.progressBar = auxView;
[auxView release];
self.progressBar.tag = 1200;
[progressView addSubview:legendLabel];
[legendLabel release];
[progressView addSubview:progressBar];
[progressBar release];
[self.view addSubview:progressView];
[progressView release];
} else {
// Create the entradilla View without any foto view space on top
UITextView *entradillaTextView = [[UITextView alloc] init];
entradillaTextView.frame = CGRectMake(26, 76, 280, 283);
entradillaTextView.text = self.evento.entradilla;
entradillaTextView.tag = 1000;
[self.view addSubview:entradillaTextView];
[entradillaTextView release];
}
// Configure the ToolBar
// Create the favourites button if evento is not favourite
UIBarButtonItem *favouritesButton;
if (!self.evento.isFavourite) {
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"29-heart" ofType:@"png"];
UIImage *addFavouriteImage = [UIImage imageWithContentsOfFile:imagePath];
favouritesButton = [[UIBarButtonItem alloc] initWithImage:addFavouriteImage style:UIBarButtonItemStylePlain target:self action:@selector(addFavoritosButtonPressed)];
// favouritesButton = [[UIBarButtonItem alloc] initWithTitle:@"+ Favoritos" style:UIBarButtonItemStyleBordered target:self action:@selector(addFavoritosButtonPressed)];
} else {
favouritesButton = [[UIBarButtonItem alloc] initWithTitle:@"- Favoritos" style:UIBarButtonItemStyleBordered target:self action:@selector(removeFavoritosButtonPressed)];
}
// Create the Web buttom
UIBarButtonItem *webButton = [[UIBarButtonItem alloc] initWithTitle:@"Mostrar Web" style:UIBarButtonItemStyleBordered target:self action:@selector(webButtonPressed)];
// Create the Map buttom
UIBarButtonItem *mapButton = [[UIBarButtonItem alloc] initWithTitle:@"Mostrar Mapa" style:UIBarButtonItemStyleBordered target:self action:@selector(mapButtonPressed)];
self.toolbarItems = [NSArray arrayWithObjects:favouritesButton, webButton, mapButton, nil];
[favouritesButton release];
[webButton release];
[mapButton release];
}
#pragma Mark -
#pragma Mark NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *resp = (NSHTTPURLResponse *) response;
if ((resp.statusCode >= 200) && (resp.statusCode < 300)) {
// OK status Code
// Prepare the temp array
self.fotoData = [NSMutableData data];
// Get the lenght of data to download for progress bar
self.dataSizeToDownload = resp.expectedContentLength;
self.dataSizeDownloaded = 0;
} else {
// Wrong status Code
self.fotoData = nil;
self.progressBar = nil;
[connection cancel];
self.fotoConnection = nil;
// Reconfigure the View
// Remove the old entradilla
[[self.view viewWithTag:1000] removeFromSuperview];
// Create the repositioned entradilla View
UITextView *entradillaTextView = [[UITextView alloc] init];
entradillaTextView.frame = CGRectMake(26, 76, 280, 283);
entradillaTextView.text = self.evento.entradilla;
entradillaTextView.tag = 1000;
[self.view addSubview:entradillaTextView];
[entradillaTextView release];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.fotoData appendData:data];
self.dataSizeDownloaded = self.dataSizeDownloaded + (float)data.length;
if ( self.dataSizeToDownload != 0) {
float prog = (float)( self.dataSizeDownloaded / self.dataSizeToDownload );
[self performSelectorOnMainThread:@selector(updateProgressBarWithValue:) withObject:[NSNumber numberWithFloat:prog] waitUntilDone:NO];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// If no error create a new View and Reload the data
self.fotoConnection = nil;
if (self.fotoData) {
self.progressBar = nil;
// !!!!!!!!! THIS IS THE EXC_BAD_ACCESS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[[self.view viewWithTag:1100] removeFromSuperview];
UIImageView *fotoImageView = [[UIImageView alloc] initWithImage:
[UIImage imageWithData:self.fotoData]];
fotoImageView.frame = CGRectMake( 26, 76, 270, 130);
fotoImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:fotoImageView];
[fotoImageView release];
[self.view reloadInputViews];
} else {
// Error downloading foto
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
self.fotoConnection = nil;
self.fotoData = nil;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:error.description message:nil delegate:nil cancelButtonTitle:@"aceptar" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void) updateProgressBarWithValue:(NSNumber *)value
{
float progress = [value floatValue];
NSLog(@"Progress %f", progress);
//self.progressBar.progress = progress;
}
-(void)加载数据到视图
{
//创建没有照片的视图
self.nombreTextView.text=self.evento.nombre;
self.fechaTextField.text=@“2010年6月12日至2011年8月3日”;
//检查是否存在照片url
if(self.evento.foto){
//下载背景图片
self.fotoConnection=[NSURLConnection connectionWithRequest:
[nsurlRequestWithURL:self.evento.foto]
代表:自我];
//错误由委托方法处理
//这种错误永远不会发生
if(!self.fotoConnection){
NSLog(@“创建连接时出错”);
出口(0);
}
//创建Dilla视图
UITextView*EntradilatextView=[[UITextView alloc]init];
entradilatextview.frame=CGRectMake(26216280145);
entradilatextview.text=self.evento.entradilla;
entradilatextview.tag=1000;
[self.view addSubview:entradilatextview];
[EntradilatextView发布];
//创建进程视图
UIView*progressView=[[UIView alloc]initWithFrame:CGRectMake(26,76,270,130)];
progressView.tag=1100;
//创建progressView标签
UILabel*legendLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,270,20)];
legendLabel.text=@“descargando fotografía”;
legendLabel.font=[UIFont systemFontOfSize:11];
legendLabel.textColor=[UIColor lightGrayColor];
legendLabel.textAlignment=UITextAlignmentCenter;
//创建progressView进度条
UIProgressView*auxView=[[UIProgressView alloc]initWithFrame:CGRectMake(35,20,200,40)];
self.progressBar=auxView;
[auxView发布];
self.progressBar.tag=1200;
[progressView添加子视图:legendLabel];
[legendLabel发布];
[progressView添加子视图:progressBar];
[进度条释放];
[self.view addSubview:progressView];
[progressView发布];
}否则{
//创建没有任何foto视图空间的entradilla视图
UITextView*EntradilatextView=[[UITextView alloc]init];
entradilatextview.frame=CGRectMake(26,76,280,283);
entradilatextview.text=self.evento.entradilla;
entradilatextview.tag=1000;
[self.view addSubview:entradilatextview];
[EntradilatextView发布];
}
//配置工具栏
//如果evento不是收藏夹,则创建收藏夹按钮
UIBAButtonim*最喜欢的按钮;
如果(!self.evento.isfavorite){
NSString*imagePath=[[NSBundle mainBundle]pathForResource:@“29心脏”类型:@“png”];
UIImage*AddFavoriteImage=[UIImage imageWithContentsOfFile:imagePath];
FavoriteSButton=[[UIBarButtonItem alloc]initWithImage:AddFavoriteImage样式:UIBarButtonItemStylePlain目标:自我操作:@selector(addFavoritosButtonPressed)];
//FavoriteSButton=[[UIBarButtonItem alloc]initWithTitle:@“+Favoritos”样式:UIBarbuttonItemStyleBorded目标:自我操作:@selector(addFavoritosButtonPressed)];
}否则{
FavoriteSButton=[[UIBarButtonItem alloc]initWithTitle:@“-Favoritos”样式:UIBarButtonItemStyleBordered目标:自我操作:@selector(removeFavoritosButtonPressed)];
}
//创建Web按钮
UIBarButtonItem*webButton=[[UIBarButtonItem alloc]initWithTitle:@“Mostrar Web”样式:UIBarButtonItem样式边界目标:自我操作:@选择器(webButtonPressed)];
//创建地图按钮
UIBarButtonItem*mapButton=[[UIBarButtonItem alloc]initWithTitle:@“Mostrar Mapa”样式:UIBarButtonItem样式有边框的目标:自我操作:@选择器(mapButtonPressed)];
self.toolbarItems=[NSArray arrayWithObjects:FavoriteSButton、webButton、mapButton、nil];
[收藏夹按钮释放];
[按钮释放];
[地图按钮释放];
}
#布拉格标记-
#pragma标记NSURLConnection委托
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
{
NSHTTPURLResponse*resp=(NSHTTPURLResponse*)响应;
如果((响应状态代码>=200)和&(响应状态代码<300)){
//正常状态代码
//准备临时数组
self.fotoData=[NSMutableData];
//获取要为进度条下载的数据长度
self.dataSizeToDownload=resp.expectedContentLength;
self.datasizedownload=0;
}否则{
//错误的状态代码
self.fotoData=nil;
self.progressBar=nil;
[连接取消];
self.fotoConnection=nil;
//重新配置视图
//除去旧的夹带物
[[self.view-withtag:1000]从SuperView移除];
//创建重新定位的Dilla视图
UITextView*EntradilatextView=[[UITextView alloc]init];
夹带剂