2012-07-11 27 views
0

我不知道为什么我的应用程序得到的错误,当我尝试在第二次[UIImage的大小]:消息发送到释放实例0x8452560

-(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 

这种情况下,任何的支持,请大家帮我

+0

你需要告诉我们你从哪里得到'rotatedOriginImage'。问题不在于'-rotateImage:'方法,问题是您给它的对象已被过度发布。 – joerick 2012-07-11 10:50:27

+0

第一次和第二次是什么意思?是在旋转之前和旋转之后?你必须检查你的rotateOriginImage.Is它在代码中的某处发布了吗? – AJS 2012-07-11 10:54:17

+0

调用方法“rotateImage”后设置“rotatedOriginImage”并返回图像。您在设置之前正在访问它的大小。 NSLog语句应该在调用“rotateImage”方法的行之后。 – AJS 2012-07-11 10:56:59

回答

2

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

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

但你必须记住释放它,以及你叫rotateImage后。所以你需要改变你的代码来做到这一点

+0

非常感谢你的支持,只是在最后添加保留,真的很神奇,非常感谢 – Makio 2012-07-12 04:41:30

+0

@Makio如果你还没有在发布中添加,那么你将会过度保留和泄漏内存。 – 2012-07-12 07:25:06

相关问题