我正在使用 CAKeyFrameAnimation 为几个 png 图像设置动画。我通过使用从一个 png 图像中剪切它们CGImageCreateWithImageInRect
当我在 XCode 中运行分析函数时,它显示了潜在的内存泄漏。他们是因为CGImageCreateWithImageInRect
. 但是当我在创建动画对象后使用 CGImageRelease 时,没有显示图像(我知道为什么,但是当我不使用 release 时,会出现泄漏)。
有人可以解释一下这个内存问题的情况吗?什么是最好的解决方案?我正在考虑为每个“剪辑”创建带有 CGImageRefs 的 UIImages。但是 CAKeyFrameAnimation 使用 CGImageRef 的字段,所以我认为没有必要创建那个 UIImages。
UIImage *source = [UIImage imageNamed:@"my_anim_actions.png"];
cutRect = CGRectMake(0*dimForImg.width,0*dimForImg.height,dimForImg.width,dimForImg.height);
CGImageRef image1 = CGImageCreateWithImageInRect([source CGImage], cutRect);
cutRect = CGRectMake(1*dimForImg.width,0*dimForImg.height,dimForImg.width,dimForImg.height);
CGImageRef image2 = CGImageCreateWithImageInRect([source CGImage], cutRect);
NSArray *images = [[NSArray alloc] initWithObjects:(__bridge id)image1, (__bridge id)image2, (__bridge id)image2, (__bridge id)image1, (__bridge id)image2, (__bridge id)image1, nil];
CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath: @"contents"];
myAnimation.calculationMode = kCAAnimationDiscrete;
myAnimation.duration = kMyTime;
myAnimation.values = images; // NSArray of CGImageRefs
[myAnimation setValue:@"ANIMATION_MY" forKey:@"MyAnimation"];
myAnimation.removedOnCompletion = NO;
myAnimation.fillMode = kCAFillModeForwards;
CGImageRelease(image1);CGImageRelease(image2);//YES or NO