2012-01-09 5 views
3

내 코드에서 이미지의 크기를 지정된 크기로 늘립니다. 코드가 지금까지 잘 작동합니다. "UIGraphicsBeginImageContext()"가 새 이미지의 메모리를 해제하지 않는다는 문제점이 있습니다. 따라서 약 10 분 후에 메모리가 가득 차고 IOS로 앱이 종료됩니다.UIGraphicsBeginImageContext는 메모리 누출을 초래합니다.

누구에게이 문제의 해결책이 있습니까? 이 코드에 의해 호출

- (CCSprite *)createStretchedSignFromString:(NSString *)string withMaxSize:(CGSize)maxSize withImage:(UIImage *)signImage 
{ 
    // Create a new image that will be stretched with 10 px cap on each side 
    UIImage *stretchableSignImage = [signImage stretchableImageWithLeftCapWidth:10 topCapHeight:10]; 

    // Set size for new image 
    CGSize newImageSize = CGSizeMake(260.f, 78.0f); 

    // Create new graphics context with size of the answer string and some cap 
    UIGraphicsBeginImageContext(newImageSize); 

    // Stretch image to the size of the answer string 
    [stretchableSignImage drawInRect:CGRectMake(0.0f, 0.0f, newImageSize.width, newImageSize.height)]; 

    // Create new image from the context 
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext(); 

    // End graphics context 
    UIGraphicsEndImageContext(); 

    // Create new texture from the stretched 
    CCTexture2D *tex = [[CCTexture2D alloc] initWithImage:resizedImage]; 

    CCSprite *spriteWithTex = [CCSprite spriteWithTexture:tex]; 

    [[CCTextureCache sharedTextureCache] removeTexture:tex]; 

    [tex release]; 

    // Return new sprite for the sign with the texture 
    return spriteWithTex; 
} 

:

// Create image from image path 
UIImage *targetSignImage = [UIImage imageWithContentsOfFile:targetSignFileName]; 

// Create new sprite for the sign with the texture 
CCSprite *plainSign = [self createStretchedSignFromString:answerString withMaxSize:CGSizeMake(260.0f, 78.0f) withImage:targetSignImage]; 

지금까지 주셔서 감사합니다.

+0

NSThread로 만들고 NSAutoreleasePool을 사용하십시오. 제 제안입니다. – SAKrisT

+1

나는 createStretchedSignFromString을 유지한다고 생각하고 해제해야합니다. * plainSign * – SAKrisT

+0

'targetSignImage'와'plainSign'은 자동으로 릴리즈되므로, 나는 그것들을 해제 할 필요가 없습니다. 나는 이미 그것을 시도했다. –

답변

2

내 문제에 대한 해결책을 찾았습니다.

먼저 위의 코드는 정확하고 누출이없는 코드입니다.

이 문제는 자식으로 planSign 인 스프라이트가 제거되어 발생했습니다. 스프라이트는 다른 스레드에서 실행되는 타이머에 의해 제거되므로 다른 NSAutoreleasePool에서도 마찬가지입니다.

[timerClass removeTarget:targetWithSign] 빈 풀을 출시했습니다.

[timerClass performSelectorOnMainThread:@selector(removeTarget:) withObject:targetWithSign waitUntilDone:NO];은 대상 스프라이트와 그 자식 plainSign을 포함하는 올바른 풀을 해제했습니다.

제안 사항에 대해 SAKrisT와 stigi에게 감사드립니다.

+0

Google에 알려 주셔서 감사합니다. :) – stigi

관련 문제