2012-07-11 59 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

相關問題