У мене є детальний перегляд. Одним із значень є URL-адреса, що вказує на зображення.
Якщо URL-адреса не дорівнює нулю, вона створює вигляд Tempory, щоб відобразити індикатор виконання та завантажити зображення з фоном NSURLConnection
. Коли він буде готовий, я знятимуть перегляд Перегляд і створити та додати новий режим перегляду зображень, коли фото буде замінено старим.
Я завжди отримую EXC_BAD_ACCESS при спробі видалити перегляд за допомогою [[self.view viewWithTag: 1100] removeFromSuperview];
. Я перевірив код на кожній лінії і не можу здогадатися, де помилка, так що, можливо, я відсутній деякі правила.
Інструменти надають мені таку інформацію:
# 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:]
І це код:
- (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;
}