2009-11-12 118 views
0

我很难解除分配包含图像的UISCrollView。 我的代码有问题,但我不明白在哪里。iphone内存释放问题与滚动视图内的图像

这是我的代码片段。它基本上创建一个循环,添加一个图像的uiscrollview并删除。 如果您使用仪器运行代码,则不会释放内存。 我还加了一些检查的retainCount,没有运气可言..

这里的代码

- (void)loadView { 
[super loadView]; 
CGRect theRect = CGRectMake(0, 0, 320, 480); 
UIView *view = [[UIView alloc] initWithFrame:theRect]; 
[view setBackgroundColor:[UIColor purpleColor] ]; 
self.view = view; 
[view release]; 

UIView *pippo = [[UIView alloc] initWithFrame:theRect]; 
[pippo setBackgroundColor:[UIColor redColor]]; 
[self.view addSubview:pippo]; 
[pippo release]; 

[self performSelector:@selector(scrollAdd:) withObject:nil afterDelay:2.0f]; 
} 



-(void)scrollAdd:(id)o { 
CGRect theRect = CGRectMake(0, 0, 320, 480); 
int numero = 1; 
scroll = [[UIScrollView alloc] initWithFrame:theRect]; 
[scroll setContentSize:CGSizeMake(320*numero,1)]; 
[scroll setScrollEnabled:YES]; 

UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"koala1b" ofType:@"jpg"]]; 
int dd = [img retainCount]; 
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect]; 
[v2 setImage:img]; 
[scroll addSubview:v2]; 
dd = [v2 retainCount]; 
[v2 release]; 
dd = [v2 retainCount]; 
dd = [img retainCount]; 
[self.view addSubview:scroll]; 
[img release]; 
dd = [img retainCount]; 

[self performSelector:@selector(scrollRemove:) withObject:nil afterDelay:2.0f]; 
} 

-(void)scrollRemove:(id)o { 
int dd = [scroll retainCount]; 
UIImageView *theV = [[scroll subviews] objectAtIndex:0]; 
dd = [theV retainCount]; 
[scroll removeFromSuperview]; 
dd = [theV retainCount]; 
[theV release]; 
dd = [theV retainCount]; 
dd = [scroll retainCount]; 
scroll = nil; 
[self performSelector:@selector(scrollAdd:) withObject:nil afterDelay:2.0f]; 
} 

回答

1

的问题是,你是释放IMG时你不应该(这可能是被屏蔽通过视图层次永远不会被释放),并且当你应该的时候你不会释放滚动。

-(void)scrollAdd:(id)o { 
CGRect theRect = CGRectMake(0, 0, 320, 480); 
int numero = 1; 
scroll = [[UIScrollView alloc] initWithFrame:theRect]; 
[scroll setContentSize:CGSizeMake(320*numero,1)]; 
[scroll setScrollEnabled:YES]; 

UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"koala1b" ofType:@"jpg"]]; 
int dd = [img retainCount]; 
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect]; 
[v2 setImage:img]; 
[scroll addSubview:v2]; 
[v2 release]; 
[self.view addSubview:scroll]; 
[img release]; 
dd = [img retainCount]; 

[self performSelector:@selector(scrollRemove:) withObject:nil afterDelay:2.0f]; 
} 

这应该是:

-(void)scrollAdd:(id)o { 
    CGRect theRect = CGRectMake(0, 0, 320, 480); 
    int numero = 1; 
    scroll = [[UIScrollView alloc] initWithFrame:theRect]; 
    [scroll setContentSize:CGSizeMake(320*numero,1)]; 
    [scroll setScrollEnabled:YES]; 

    UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"koala1b" ofType:@"jpg"]]; 
    UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect]; 
    [v2 setImage:img]; 
    [scroll addSubview:v2]; 
    [v2 release]; 
    [self.view addSubview:scroll]; 

    [self performSelector:@selector(scrollRemove:) withObject:nil afterDelay:2.0f]; 
} 

当然,如果你这样做,你需要也使您的视图去除路径略有变化:

-(void)scrollRemove:(id)o { 
    [scroll removeFromSuperview]; 
    [scroll release]; 
    scroll = nil; 

[self performSelector:@selector(scrollAdd:) withObject:nil afterDelay:2.0f]; 
} 
1

我已经基本达成与@Louis的结论相同,但也在代码中对我删除的内容和原因做了一些评论。

- (void)loadView { 
[super loadView]; 
CGRect theRect = CGRectMake(0, 0, 320, 480); 
UIView *view = [[UIView alloc] initWithFrame:theRect]; 
[view setBackgroundColor:[UIColor purpleColor] ]; 
self.view = view; 
[view release]; 

UIView *pippo = [[UIView alloc] initWithFrame:theRect]; 
[pippo setBackgroundColor:[UIColor redColor]]; 
[self.view addSubview:pippo]; 
[pippo release]; 

[self performSelector:@selector(scrollAdd:) withObject:nil afterDelay:2.0f]; 
} 



-(void)scrollAdd:(id)o { 
    CGRect theRect = CGRectMake(0, 0, 320, 480); 
    int numero = 1; 
    scroll = [[UIScrollView alloc] initWithFrame:theRect]; 
    [scroll setContentSize:CGSizeMake(320*numero,1)]; 
    [scroll setScrollEnabled:YES]; 

    // img is already autoreleased, you're releasing again down below 
    // --- UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"koala1b" ofType:@"jpg"]]; 
    UIImageView *v2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"koala1b.jpg"]]; 
    v2.frame = theRect; 
    //--- [v2 setImage:img]; 
    [scroll addSubview:v2]; 
    [v2 release]; 
    [self.view addSubview:scroll]; 
    // NO !; img is autoreleased from imageWithContentsOfFile 
    //--- [img release]; 

    [self performSelector:@selector(scrollRemove:) withObject:nil afterDelay:2.0f]; 
} 

-(void)scrollRemove:(id)o { 
    //--- UIImageView *theV = [[scroll subviews] objectAtIndex:0]; 
    [scroll removeFromSuperview]; 
    // you don't own theV because you didn't create it here, or send it a retain. So NO release 
    // it also appears that you think you're responsible for releasing or cleaning up 
    // a view's subviews. NO. Just call [scroll removeFromSuperview] and let scroll view 
    // clean it's own resources. 
    //--- [theV release]; 
    [scroll release]; 
    scroll = nil; 
    [self performSelector:@selector(scrollAdd:) withObject:nil afterDelay:2.0f]; 
} 
+0

谢谢,您的解决方案不会增加内存使用量! 这真的很有用 – 2009-11-12 14:26:31