J'ai un problème de mémoire lié à UIImageView. Après avoir ajouté cette vue à mon UIScrollView, si j'essaie de libérer le UIImageView l'application se bloque. Selon la trace de la pile, quelque chose appelle [UIImageView stopAnimating] après l'appel de [UIImageView dealloc]. Cependant, si je ne libère pas la vue, la mémoire n'est jamais libérée, et j'ai confirmé qu'il reste un appel de retenue supplémentaire sur la vue après la désallocation ... ce qui provoque une augmentation rapide de mes allocations totales et finit par bloquer l'application après avoir chargé la vue plusieurs fois. Je ne suis pas sûr de ce que je fais mal ici ... Je ne sais pas ce qui essaie d'accéder à UIImageView après sa sortie. J'ai inclus l'en-tête et le code d'implémentation ci-dessous (j'utilise le framework Three20, si cela a quelque chose à voir avec ça ... AppScrollView est juste un UIScrollView qui transfère l'évènement touchesEnded au répondeur suivant):Problème lors de la libération de UIImageView après l'ajout à UIScrollView
tête:
@interface PhotoHiResPreviewController : TTViewController <UIScrollViewDelegate> {
NSString* imageURL;
UIImage* hiResImage;
UIImageView* imageView;
UIView* mainView;
AppScrollView* mainScrollView;
}
@property (nonatomic, retain) NSString* imageURL;
@property (nonatomic, retain) NSString* imageShortURL;
@property (nonatomic, retain) UIImage* hiResImage;
@property (nonatomic, retain) UIImageView* imageView;
- (id)initWithImageURL:(NSString*)imageTTURL;
Mise en œuvre:
@implementation PhotoHiResPreviewController
@synthesize imageURL, hiResImage, imageView;
- (id)initWithImageURL:(NSString*)imageTTURL {
if (self = [super init]) {
hiResImage = nil;
NSString *documentsDirectory = [NSString stringWithString:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
[self setImageURL:[NSString stringWithFormat:@"%@/%@.jpg", documentsDirectory, imageTTURL]];
}
return self;
}
- (void)loadView {
[super loadView];
// Initialize the scroll view
hiResImage = [UIImage imageWithContentsOfFile:self.imageURL];
CGSize photoSize = [hiResImage size];
mainScrollView = [[AppScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
mainScrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
mainScrollView.backgroundColor = [UIColor blackColor];
mainScrollView.contentSize = photoSize;
mainScrollView.contentMode = UIViewContentModeScaleAspectFit;
mainScrollView.delegate = self;
// Create the image view and add it to the scrollview.
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
tempImageView.contentMode = UIViewContentModeCenter;
[tempImageView setImage:hiResImage];
self.imageView = tempImageView;
[tempImageView release];
[mainScrollView addSubview:imageView];
// Configure zooming.
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat widthRatio = screenSize.width/photoSize.width;
CGFloat heightRatio = screenSize.height/photoSize.height;
CGFloat initialZoom = (widthRatio > heightRatio) ? heightRatio : widthRatio;
mainScrollView.maximumZoomScale = 3.0;
mainScrollView.minimumZoomScale = initialZoom;
mainScrollView.zoomScale = initialZoom;
mainScrollView.bouncesZoom = YES;
mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
mainView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
mainView.backgroundColor = [UIColor blackColor];
mainView.contentMode = UIViewContentModeScaleAspectFit;
[mainView addSubview:mainScrollView];
// Add to view
self.view = mainView;
[imageView release];
[mainScrollView release];
[mainView release];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
- (void)dealloc {
mainScrollView.delegate = nil;
TT_RELEASE_SAFELY(imageURL);
TT_RELEASE_SAFELY(hiResImage);
[super dealloc];
}
Je ne suis pas sûr de savoir comment contourner ce problème. Si je supprime l'appel à [imageView release] à la fin de la méthode loadView tout fonctionne correctement ... mais j'ai des allocations massives qui grimpent rapidement à un point de rupture. Si je le libère, cependant, il y a cet appel [UIImageView stopAnimating] qui bloque l'application après la désallocation de la vue.
Merci pour toute aide! Je me suis cogné la tête contre celui-ci pendant des jours. :-P
Cheers, Josias
Cela l'a fait! Merci beaucoup! –