2015-01-12 5 views
2

UIImage의 배열에 CAKeyframeAnimation으로 애니메이션을 적용하려고합니다. 쉬운 이론.
게시물 하단에 샘플 코드가 있습니다.CAKeyframeAnimation - 이미지 배열에 애니메이션을 적용하면 완료 후 큰 할당이 생성됩니다.

내 문제는 애니메이션이 끝난 후에는 제거 할 수없는 막대한 누출이 있다는 것입니다.

코드 CAKeyframeAnimation를 초기화하기 위해서 그런 다음

... // Code to change 

keyframeAnimation.delegate = self; 

// keyframeAnimation.removedOnCompletion = YES; 
keyframeAnimation.removedOnCompletion = NO; 
keyframeAnimation.fillMode = kCAFillModeForwards; 

.... 

:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 
{ 
    if (flag) 
    { 
     [self.animationImageView.layer removeAllAnimations]; 
     [self.animationImageView.layer removeAnimationForKey:@"flingAnimation"]; // just in case 
    } 
} 

- (void)animateImages 
{ 
    CAKeyframeAnimation *keyframeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"contents"]; 
    keyframeAnimation.values = self.imagesArray; // array with images 

    keyframeAnimation.repeatCount = 1.0f; 
    keyframeAnimation.duration = 5.0; 

    keyframeAnimation.removedOnCompletion = YES; 

    CALayer *layer = self.animationImageView.layer; 

    [layer addAnimation:keyframeAnimation 
      forKey:@"flingAnimation"]; 
} 

애니메이션에 대리인을 추가 및 애니메이션을 제거 수동으로 같은 누출 효과의 원인

결과는 다음과 같습니다. 엄청난 배당금. 메모리 적층 체의 크기는 이미지의 크기에 비례한다 :

enter image description here

I uploaded an example to GitHub to check the code.

+0

나는 악기를 실행하고 거기에는 누수가 없지만 할당이 높다. – gabbler

+0

높은 할당량을 해결하는 방법을 알고 있습니까? –

+0

대신'imageWithContentsOfFile'을 사용하십시오. – gabbler

답변

3

I가 문제 발견

를 해결했다.

gabbler은 누설 문제가 없다고 말하고있었습니다. 문제는 높은 이미지 할당이었습니다.

이미지와 함께 배열을 릴리스했지만 이미지가 메모리에서 사라지지 않았습니다.

[UIImage imageNamed:@""]; 

방법 정의에서 :

그래서 결국 나는 문제를 발견

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method locates and loads the image data from disk or asset catelog, and then returns the resulting object. You can not assume that this method is thread safe.

그래서, imageNamed: 저장하는 개인 캐시에있는 이미지입니다.
- 첫 번째 문제는 캐시 크기를 제어 할 수 없다는 것입니다.
- 두 번째 문제는 캐시가 제 시간에 정리되지 않았고 많은 이미지를 imageNamed:과 함께 할당하는 경우 응용 프로그램이 중단되는 것입니다.

해결 :

가 번들에서 이미지를 직접 할당 :

NSString *imageName = [NSString stringWithFormat:@"imageName.png"]; 
NSString *path = [[NSBundle mainBundle] pathForResource:imageName 

// Allocating images with imageWithContentsOfFile makes images to do not cache. 
UIImage *image = [UIImage imageWithContentsOfFile:path]; 

작은 문제 : Images.xcassets에서

이미지는 할당되지 않습니다 얻을. 따라서 이미지를 Images.xcassets 외부로 이동하면 Bundle에서 직접 할당 할 수 있습니다.

Example project with solution here.

+0

300 이미지를 추가하면 솔루션도 작동하지 않습니다 : D –

+0

300 이미지 !!! 대신 비디오를 사용하는 것이 좋습니다. –

+0

그러나 CALayer를 추가하고 CAKeyframeAnimation을 넣어서 AVMutableVideoComposition의 도움으로 비디오에 이미지를 넣어야하므로 비디오를 사용할 수 없습니다. (비디오를 사용하면 PNG 이미지처럼 알파가 생기지 않습니다.) UI에 애니메이션을 표시해야하지만 제 경우에는 비디오에 애니메이션을 저장해야하는 경우 비디오 사용에 대한 제안이 좋습니다 .. 사전에 도움을 주셔서 감사합니다 :) –