2011-09-25 3 views
1

아래의 코드는 스프라이트를 자르는 방법을 보여 주지만, 메모리 사용은 지속적으로 증가합니다. 어떻게 해결할 수 있습니까?스프라이트 시트의 문제점

CGImageRef imgRef = [imgSprite CGImage]; 
[imgView setImage:[UIImage imageWithCGImage:CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height))]]; 
CGImageRelease(imgRef); 

이 코드는 0.1 간격으로 NSTimer에 의해 호출됩니다.

답변

2

imgSprite 선언을 게시하지 않았으므로 해당 클래스가 Cocoa 명명 규칙을 따른다고 가정합니다. 에서

:

CGImageRef imgRef = [imgSprite CGImage]; 

하는 방법 (비 밀고 자 1 방법) 따라서 당신 해야하지 릴리스에게 그것을 소유하지 할 개체를 반환합니다. 에서

:

CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height)) 

CGImageCreateWithImageInRect() 당신이 자신 할 하는 이미지를 돌려줍니다 (이름이 규칙 만들기 2을 다음과 함수) :

[imgView setImage:[UIImage imageWithCGImage:CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height))]]; 

인수는 표현이다 따라서 이어야합니다. 그렇지 않으면 해제하십시오. 에서

:

CGImageRelease(imgRef); 

당신은, 당신은 자신의하지이미지를 해제하고 당신 해야하지 릴리스 그것 때문에.

두 가지 문제가 있습니다. 즉, imgRef을 넘기고 (CGImageCreateWithImageInRect()에 의해 반환 된 이미지가 누출 됨) 두 가지 문제가 있습니다.

당신은 대신 다음을 수행해야합니다

// you do not own imgRef, hence you shouldn’t release it 
CGImageRef imgRef = [imgSprite CGImage]; 

// use a variable for the return value of CGImageCreateWithImageInRect() 
// because you own the return value, hence you should release it later 
CGImageRef imgInRect = CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height)); 

[imgView setImage:[UIImage imageWithCGImage:imgInRect]]; 

CGImageRelease(imgInRect); 

당신은 Memory Management Programming GuideMemory Management Programming Guide for Core Foundation 읽어보십시오.

1 밀고 자는 ALLOC, 당신은 이름이 생성 포함 또는 복사 한 다음 반환 값을 자신의 함수를 호출하는 경우 있음, 따라서 당신이 그것을 해제해야

2Create Rule 상태를 복사, 새로운 유지 = 더 이상 필요하지 않을 때.