2010-08-25 73 views
3

我有一个非常大的图像IntroViewController,这增加了我的应用程序的内存大约1.5MB。该图像设置在视图控制器的NIB中的UIImageView上。我的应用程序如何在内存中显示图像?

一旦介绍完成后,我会在IntroViewController上调用release,然后它自己成功调用dealloc并在大型UIImageView上调用release。然而,我在乐器中的记忆似乎没有再次回落。我可以通过将图像重命名为NIB中的UIImageView来测试内存差异,以便找不到任何内容,然后内存下降大约1.5MB。

那么,为什么我不能回忆那些回忆呢?是否有什么,我想念正在停止UIImageView正确dealloc'd?

感谢,

:-Joe

-------增加:CODE ------

#import <UIKit/UIKit.h> 

@class AppDelegate_iPhone; 

@interface IntroViewController : UIViewController 
{ 
    AppDelegate_iPhone *appDelegate; 

    UIImageView *wheelImageView; 
    UIView *copyOverlayView; 
} 

@property (nonatomic, retain) IBOutlet UIImageView *wheelImageView; 
@property (nonatomic, retain) IBOutlet UIView *copyOverlayView; 

- (void)startAnimation; 

@end 

@implementation IntroViewController 

@synthesize wheelImageView, copyOverlayView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate]; 
    [self startAnimation]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [self.wheelImageView removeFromSuperview]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    self.wheelImageView.image = nil; 
    appDelegate = nil; 
} 

- (void)dealloc 
{ 
    [wheelImageView release]; 
    [copyOverlayView release]; 

    [super dealloc]; 
} 

- (void)startAnimation 
{ 
    self.wheelImageView.transform = CGAffineTransformMakeScale(0.5, 0.5); 
    [UIView beginAnimations:@"wheelScale" context:nil]; 
    [UIView setAnimationDuration:2.0]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(wheelScaleDidFinish)]; 
    self.wheelImageView.transform = CGAffineTransformMakeScale(1.0, 1.0); 
    [UIView commitAnimations]; 

    [UIView beginAnimations:@"copyOverlayFade" context:nil]; 
    [UIView setAnimationDuration:1.0]; 
    [UIView setAnimationDelay:1.0]; 
    self.copyOverlayView.alpha = 0; 
    [UIView commitAnimations]; 
} 

- (void)wheelScaleDidFinish 
{ 
    [appDelegate introAnimationFinished]; 
} 

@end 

----编辑

灿任何人都请帮忙?我很难过,这导致我的应用程序崩溃在iPhone上由于高内存:(无法弄清楚如何摆脱愚蠢的形象!Grrrr ...

回答

0

好吧,所以我找到了解决方案,但我非常惊讶!

我不仅需要将UIImageView移出IB并动态创建它,我必须避免使用便捷方法来加载图像。相反的:

wheelImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wheel-intro.png"]; 
[self.view addSubview:self.wheelImageView]; 
[wheelImageView release]; 

我不得不使用:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"wheel-intro" ofType:@"png"]; 
wheelImage = [[UIImage alloc] initWithContentsOfFile:imagePath]; 
wheelImageView = [[UIImageView alloc] initWithImage:wheelImage]; 
[wheelImage release]; 
[self.view addSubview:self.wheelImageView]; 
[wheelImageView release]; 

现在,当我在viewDidDisappear处理程序做[wheelImageView removeFromSuperview],一切都得到正确清理。

我发现真正奇怪的是,将图像放入界面生成器在内存管理中与在XCode中使用便捷方法相同。既不能正常工作!注意自我:停止使用方便的方法...

+1

原因是UIImage imageNamed:和朋友维护一个图片缓存。在压力下,系统将释放内存。 – 2010-08-28 07:05:50

+0

所以虽然有autorelease,但有些对象有自己的缓存?这是一个鲜为人知的事实,谢谢。但是,你说在压力下系统会释放内存,但在实践中,对于我来说,手机会因内存不足而崩溃,并且泄漏仍会显示缓存的图像! – jowie 2010-08-28 12:17:55

1

UIImageView正在释放,但你确定你没有在其他地方留下幻影吗?如果我是你,我会试着追踪它的保留和发布。你可以用时髦的调试技巧,像malloc_history这样的工具,或者简单地通过make a的UIImageView的子类:

@interface DebugImageView @end 
@implementation DebugImageView 
    - (id) retain { 
    NSLog(@"retained"); 
    return [super retain]; 
    } 

    - (void) release { 
    NSLog(@"release"); 
    [super release]; 
    } 

    - (void) dealloc { 
    NSLog(@"dealloc"); 
    [super dealloc]; 
    } 

只要改变类的UIImageView对象在IB到DebugImageView,你应该是好去你也可以把破发点上的,看看他们是所谓的,或只是让。请拨打电话backtrace()我所在的地方日志报表。

如果该对象实际上被删除,那么你的内存增加可能并不严重,很可能是由于大量可重用缓冲区对象被延迟驱逐或堆碎片。尽管1.5MB似乎有点高,所以我怀疑这是一个过度保留。

+0

谢谢 - 我试过了,我看到很多痕迹!尽管如此,它似乎在调用“dealloc”。它可能是因为它是UIView动画的一部分,它保存在图像上?我是否需要释放或取消委托UIView动画序列? – jowie 2010-08-25 23:19:21

+0

PS在泄漏,我可以看到,它仍然挂在 - 我有一个1.39MB的Malloc与负责任的图书馆ImageIO ... – jowie 2010-08-25 23:26:36

+0

这是可以想象的图像可以复制到一个缓冲区支持CALayer,你保留间接无意中保留了动画的某些部分,但没有更多的细节,这很难弄清楚。另外,你使用的是什么init方法,是否有机会保存用于创建图像的缓冲区(如NSData)? – 2010-08-26 02:10:47

相关问题