UIImageView奇怪崩溃

时间:2013-08-01 13:04:56

标签: iphone objective-c uiimageview

这是代码:

-(void)initView
{
        increaseImage = [[[UIImageView alloc] initWithFrame:CGRectMake(277, 189, 511, 406)]autorelease];
         ..
         ..
         increaseImage.image = [UIImage imageNamed:[imgarray lastObject]];
         ..
         ..
}


-(void)cleanUpView
{

    if(increaseImage)
    {
        if([increaseImage superview]!=nil )
            [increaseImage removeFromSuperview];    //Crashes Here

        increaseImage = nil;
    }
}

在removeFromSuperview线崩溃了......如何避免崩溃?

备注:     不使用ARC     我想解决autorelease问题..为什么崩溃...它适用于其他对象。使用了大量的视图因此很难改变自动释放。所以希望找到正确的理由......

2 个答案:

答案 0 :(得分:2)

autorelease的分配时删除increaseImage,如下所述..

替换此行

increaseImage = [[[UIImageView alloc] initWithFrame:CGRectMake(277, 189, 511, 406)]autorelease];

与波纹线

increaseImage = [[UIImageView alloc] initWithFrame:CGRectMake(277, 189, 511, 406)];

答案 1 :(得分:1)

请尝试这样:

-(void)initView
 {
        increaseImage = [[UIImageView alloc] initWithFrame:CGRectMake(277, 189, 511, 406)];
         ..
         ..
        increaseImage.image = [UIImage imageNamed:[imgarray lastObject]];
         ..
         ..
 }


-(void)cleanUpView
 {

   if(increaseImage)
   {
      if([increaseImage superview]!=nil )
        [increaseImage removeFromSuperview];    
        [increaseImage release];  // release here instead of autorelease
        increaseImage = nil;
   }
 }