1

我正在尝试合并四个图像以使其成为一个 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

4

1 回答 1

1

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

尝试:

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();

反而。

于 2013-08-12T19:25:43.523 回答