I'm making an app to manipulate video, now I m adding sprite animation above the video.
For that I m using CAKeyframeAnimation, I have a method in a Manager that is adding the animated sprite.
// add effect on the video
[EffectManager addEffectOnVideoAtURL:outputURL handler:^{
NSLog(@"video exported with aimations");
}];
The thing is that when this videoURL is stored in the document folder of my app, the CAKeyframeAnimation is not visible.
But when I first export the video to the Library/camera roll, and then load and add the effect on this exported video, they are visible.
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// export the video to the library/camera roll
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputURL]) {
[library writeVideoAtPathToSavedPhotosAlbum:outputURL completionBlock:^(NSURL *assetURL, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
// add effect on the video
[EffectManager addEffectOnVideoAtURL:assetURL handler:^{
NSLog(@"video exported with aimations");
}];
});
}];
}
This code, using the library is working correctly, but I don t want to have to save a intermediate video to the library.
I have tried to use the first snippet of code in a async way, but the result is the same.
I have tried also to load a movie from the library, and use the first snippet of code, but same result, no CAKeyframeAnimation.
Any idea why CAKeyframeAnimation is visible only when the video is first exported to the lbrary/photo roll ?
It finally has nothing to do with PhotoScroll.
The CAKeyFrameAnimation.removedOnCompletion is by default set on YES. When it is added to the CALayer, its confusing the videoexport.
So by setting it to NO, CAKeyFrameAnimation is visible on the animation.
NSMutableArray* animationArray = [NSMutableArray array];
UIImage*img1 = [UIImage imageWithContentsOfFile:path];
[animationArray addObject:(__bridge id)[img1 CGImage]];
UIImage*img2 = [UIImage imageWithContentsOfFile:path];
[animationArray addObject:(__bridge id)[img2 CGImage]];
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
[animation setCalculationMode:kCAAnimationDiscrete];
[animation setDuration:0.5f];
[animation setRepeatCount:HUGE_VALF];
[animation setValues:[[NSArray alloc] initWithArray:animationArray]];
// this line does the fix:
animation.removedOnCompletion = NO;