CFRelease导致iPad应用程序崩溃

时间:2013-08-12 18:44:31

标签: ios ipad automatic-ref-counting cgimageref uigraphicscontext

我正在尝试合并四个图像,使其成为一个uiimage。但是当我要做CFRelease时,它正在崩溃。如果我不这样做,那么它会导致内存泄漏并崩溃。怎么能解决这个问题?

请参阅下面的代码。

    referenceView.frame = CGRectMake(0, 0, 1024, 768);
    referenceView.hidden = NO;

    [self.view insertSubview:referenceView belowSubview:selfView];

    imgView1.image = delegate.imageCroped;
    imgView2.image = [UIImage imageNamed:@"videobg.png"];
    imgView3.image = [UIImage imageNamed:@"videochar.png"];;
    imgView4.image = smileView.image;

    imgView1.frame = CGRectMake(456, 311, delegate.imageCroped.size.width-30.8, delegate.imageCroped.size.height-40);
    imgView2.frame = CGRectMake(0, 0, 1024, 768);
    imgView3.frame = CGRectMake(392, 187, imgView3.image.size.width, imgView3.image.size.height);
    imgView4.frame = CGRectMake((smileView.frame.origin.x-((smileView.frame.origin.x > 530) ?    30  : 15)), (smileView.frame.origin.y+((smileView.frame.origin.y <= 307) ?    95  : 50)), smileView.frame.size.width, smileView.frame.size.height);

    [referenceView addSubview:imgView1];
    [referenceView addSubview:imgView2];
    [referenceView addSubview:imgView3];
    [referenceView addSubview:imgView4];

    UIGraphicsBeginImageContext(referenceView.bounds.size);
    [referenceView.layer renderInContext:UIGraphicsGetCurrentContext()];
    CGImageRef finalImage = ( CGImageRef)(UIGraphicsGetImageFromCurrentImageContext());
    UIGraphicsEndImageContext();

    [referenceView removeFromSuperview];
    [imgView1 removeFromSuperview];
    [imgView2 removeFromSuperview];
    [imgView3 removeFromSuperview];
    [imgView4 removeFromSuperview];

    UIImage *resizedImage = [UIImage imageWithCGImage:finalImage];

    CFRelease(finalImage);

    return resizedImage;

}
@catch (NSException *exception) {

}

我收到以下错误。我该如何解决这个问题?我的项目在ARC。

  

* - [UIImage _cfTypeID]:发送到解除分配的实例0x1e88dce0的消息

1 个答案:

答案 0 :(得分:1)

UIGraphicsGetImageFromCurrentImageContext()返回UIImage *,而不是CGImageRef。并且您没有拥有生成的UIImage,因此您不想释放它(或CFRelease()它。)

尝试:

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();

代替。