[UIImage size]:发送到解除分配的实例0x8452560的消息

时间:2012-07-11 10:30:35

标签: iphone objective-c rotation

我不知道为什么我的应用程序在第二次尝试旋转图像时出现错误

-(UIImage *)rotateImage:(UIImage *)image{
    // calculate the size of the rotated view's containing box for our drawing space
    CGSize rotatedSize = image.size;

    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, DegreesToRadians(90));

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-image.size.width / 2, -image.size.height / 2, image.size.width, image.size.height), image.CGImage);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

我通过以下方式调用此函数:

 NSLog(@"%f %f",rotatedOriginImage.size.width,rotatedOriginImage.size.height);
    rotatedOriginImage = [self rotateImage:rotatedOriginImage];

第一次登录时:

2012-07-11 17:22:50.825 meshtiles[3330:707] 600.000000 600.000000

但是第二次:

2012-07-11 17:22:55.253 meshtiles[3330:707] *** -[UIImage size]: message sent to deallocated instance 0x8452560

对此案件的任何支持,请帮助我

1 个答案:

答案 0 :(得分:2)

如果您不使用ARC,那么您将从方法返回自动释放的对象newImage。你需要保留它。

rotatedOriginImage = [[self rotateImage:rotatedOriginImage] retain];

但是你必须记得在调用rotateImage之后释放它。所以你需要改变你的代码才能做到这一点