我看到1FPS试图在两个375x667图像上使用CoreImage CIFlashTransition,我该怎么做才能加快速度呢?

时间:2015-04-08 23:30:38

标签: ios animation core-image

我将两个不起眼的UIViews捕获为不透明图像,并尝试使用CIFlashTransition为转换设置动画。我得到的东西非常像1FPS。我是在做一些愚蠢的事情,还是我不应该期待CoreImage的动画级别的表现?

过滤器和CALayer在Swift中设置,CALayer(子类)是ObjC。

过滤器设置如下:

let extent = CIVector(x: 0, y: 0, z: sourceView.frame.size.width, w: sourceView.frame.size.height)
filter.setDefaults()
filter.setValue(extent, forKey: kCIInputExtentKey)
filter.setValue(CIColor(red: 0, green: 0, blue: 0), forKey: kCIInputColorKey)
filter.setValue(self.sourceView.renderImage().CIImageRenderIfNeeded(), forKey: kCIInputImageKey)
filter.setValue(self.destinationView.renderImage().CIImageRenderIfNeeded(), forKey: kCIInputTargetImageKey)
filter.setValue(0.0, forKey: kCIInputTimeKey)

图层看起来像:

self.displayLayer = CACITransitionLayer(layer: self.animationView.layer, transitionFilter: filter)
self.displayLayer.frame = self.animationView.bounds
self.displayLayer.bounds = self.animationView.bounds
self.displayLayer.backgroundColor = UIColor(hue: 0.3, saturation: 0.7, brightness: 0.7, alpha: 0.4).CGColor
self.displayLayer.time = 1.0
self.animationView.layer.addSublayer(self.displayLayer)

CACITransitionLayer标题非常简单:

#import <Foundation/Foundation.h>
#import <CoreImage/CoreImage.h>
#import <QuartzCore/QuartzCore.h>

@interface CACITransitionLayer : CALayer

- (instancetype)initWithLayer:(id)layer transitionFilter:(CIFilter*)filter;


@property (nonatomic, assign) CGFloat time;
@property (nonatomic, strong) CIFilter *transitionFilter;
@property (nonatomic, strong) CIContext *context;

@end

它的代码:

#import "CACITransitionLayer.h"

@implementation CACITransitionLayer

@dynamic time;

- (instancetype)initWithLayer:(id)layer transitionFilter:(CIFilter *)filter {
  self = [super initWithLayer:layer];
  if (self) {
    self.transitionFilter = filter;
    self.time = 0;
    self.context = [CIContext contextWithOptions:nil];
    self.contentsScale = 2;
  }
  return self;
}

- (id<CAAction>)actionForKey:(NSString *)key
{
  if ([key isEqualToString:@"time"])
  {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.fromValue = @(self.time);
    animation.duration = 10; // For debug
    return animation;
  }
  return [super actionForKey:key];
}

+ (BOOL)needsDisplayForKey:(NSString *)key
{
  if ([@"time" isEqualToString:key])
  {
    return YES;
  }
  return [super needsDisplayForKey:key];
}

- (void)display
{
  NSLog(@"time: %f", [[self presentationLayer] time]);
  [self.transitionFilter setValue:@([self.presentationLayer time]) forKey:kCIInputTimeKey];
  CIImage *filteredImage = [self.transitionFilter valueForKey:kCIOutputImageKey];
  CGRect doubleSize = self.bounds;
  doubleSize.size.width *= 2;
  doubleSize.size.height *= 2;
  CGImageRef usefulFilteredImage = [self.context createCGImage:filteredImage fromRect:doubleSize];
  self.contents = (__bridge id)(usefulFilteredImage);
}

@end

我最近一次运行的日志:

2015-04-08 16:13:42.879 Darkenss[5999:4856938] time: 0.000000
2015-04-08 16:13:44.843 Darkenss[5999:4856938] time: 0.000217
2015-04-08 16:13:45.554 Darkenss[5999:4856938] time: 0.071336
2015-04-08 16:13:46.292 Darkenss[5999:4856938] time: 0.145042
2015-04-08 16:13:47.976 Darkenss[5999:4856938] time: 0.313537
2015-04-08 16:13:49.860 Darkenss[5999:4856938] time: 0.501858
2015-04-08 16:13:50.824 Darkenss[5999:4856938] time: 0.598269
2015-04-08 16:13:51.783 Darkenss[5999:4856938] time: 0.694170
2015-04-08 16:13:52.776 Darkenss[5999:4856938] time: 0.793530
2015-04-08 16:13:53.780 Darkenss[5999:4856938] time: 0.893871
2015-04-08 16:14:00.002 Darkenss[5999:4856938] time: 1.000000

1 个答案:

答案 0 :(得分:0)

perf很可能很慢,因为您使用CI在每个渲染上创建CGImage。您是否可以重新构建视图,以便为CI提供EAGLContext以呈现?