0

我有以下方法UIImageManipulation.m

+(UIImage *)scaleImage:(UIImage *)source toSize:(CGSize)size
{
    UIImage *scaledImage = nil;
    if (source != nil)
    {
        UIGraphicsBeginImageContext(size);
        [source drawInRect:CGRectMake(0, 0, size.width, size.height)];
        scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    return scaledImage;
}

我用不同的观点来称呼它:

imageFromFile = [UIImageManipulator scaleImage:imageFromFile toSize:imageView.frame.size];

(imageView是之前分配的一个UIImageView)

这在我的代码中效果很好。我完美地调整了图像的大小,并且抛出了零错误。我也没有在构建->分析下弹出任何东西。但是第二次我打开NSZombieEnabled调试另一个EXC_BAD_ACCESS问题时,代码就中断了。每一次。我可以NSZombieEnabled关闭,代码运行良好。我打开它,然后砰的一声。破碎的。我注释掉这个电话,它又可以工作了。每次,它都会在控制台中给我一个错误:-[UIImage release]: message sent to deallocated instance 0x3b1d600. 如果 `NSZombieEnabled 关闭,则不会出现此错误。

有任何想法吗?

- 编辑 -

好吧,这要死我了。我到处都设置了断点,但我仍然无法抓住这个东西。这是我调用该scaleImage方法时的完整代码:

-(void)setupImageButton
{
    UIImage *imageFromFile;

    if (object.imageAttribute == nil) {
        imageFromFile = [UIImage imageNamed:@"no-image.png"];
    } else {
        imageFromFile = object.imageAttribute;
    }
    UIImage *scaledImage = [UIImageManipulator scaleImage:imageFromFile toSize:imageButton.frame.size];
    UIImage *roundedImage = [UIImageManipulator makeRoundCornerImage:scaledImage :10 :10 withBorder:YES];
    [imageButton setBackgroundImage:roundedImage forState:UIControlStateNormal];
}

另一种方法UIImageManipulatormakeRoundCornerImage_

不过,这与这种方法有关。必须。如果我将其注释掉,它会很好用。如果我把它留在里面,错误。但它不会在NSZombieEnabled关闭时抛出错误。

4

2 回答 2

3

NSZombieEnabled 的目的是检测在对象被释放后发送给对象的消息。您看到的控制台错误是 NSZombieEnabled,告诉您一条release消息正在发送到UIImage. 通常像这样的错误是调用过多release或调用不足的结果retain

在这种情况下,您的scaleImage:toSize:方法返回一个 autoreleased UIImage。您从 NSZombieEnabled 收到的错误消息表明您可能会在返回此对象后释放它。这可以解释你的错误。当您的自动释放池耗尽时,它会尝试释放已被释放的对象。

You're passing imageFromFile to scaleImage:toSize:, and then reassigning that same variable to the return value. There's nothing wrong with this idiom per se, but does require some extra care to avoid memory bugs like this one. You're overwriting your reference to the original object, so you either have to make sure it's autoreleased before the assignment, or save a separate reference that you can manually release after the assignment. Otherwise your original object will leak.

于 2010-06-07T21:06:22.437 回答
0

The error was due to a release going on in the makeRoundedCornerImage method from UIImageManipulator. Still not sure why it wasn't getting picked up without NSZombieEnabled turned on, but that's what it was.

You can find the offending line in the Gist I posted in the original question: Line 74.

于 2010-06-10T22:22:06.960 回答