【问题标题】:How to Add GIf image as layer on view?如何将 GIf 图像添加为视图层?
【发布时间】:2016-09-30 06:35:36
【问题描述】:

我正在做一个录制视频的项目。
录制完成后,我使用 AVPlayer 添加图层来播放视频。
现在我想要一个 gif 图像作为该视图的添加图层。

我成功地将 gif 图像添加为图层,但图像没有动画。它就像一张静态图片。
我使用Library 来制作 GIF 图片。

我的代码在下面

self.avPlayer = [AVPlayer playerWithURL:self.urlstring];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.preview_view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[self.preview_view.layer addSublayer:videoLayer];


NSURL *url = [[NSBundle mainBundle] URLForResource:@"02" withExtension:@"gif"];

CALayer *layer = [[CALayer alloc]init];
layer.frame = self.img_gif.bounds;
layer.contents = (id) [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]].CGImage;
[self.preview_view.layer addSublayer:layer];

请帮帮我

谢谢

【问题讨论】:

  • 我相信你必须使用 startAnimating 来显示图像动画。
  • 但是 startanimating 与 UIimageview 一起使用。我使用 CALayer 将图像添加为视图层
  • 如果让 layer.contents 等于包含 gif 的 UIImageView 并为 gif 设置动画怎么办?
  • 还是一样的结果。图片没有动画

标签: ios objective-c calayer


【解决方案1】:

我已经成功地做到了。我将 GIF 图片添加到图层。

- (CAKeyframeAnimation *)createGIFAnimation:(NSData *)data{

    CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)(data), nil);
    int frameCount =(int) CGImageSourceGetCount(src);

    // Total loop time
    float time = 0;

    // Arrays
    NSMutableArray *framesArray = [NSMutableArray array];
    NSMutableArray *tempTimesArray = [NSMutableArray array];

    // Loop
    for (int i = 0; i < frameCount; i++){

        // Frame default duration
        float frameDuration = 0.1f;

        // Frame duration
        CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(src,i,nil);
        NSDictionary *frameProperties = (__bridge NSDictionary*)cfFrameProperties;
        NSDictionary *gifProperties = frameProperties[(NSString*)kCGImagePropertyGIFDictionary];

        // Use kCGImagePropertyGIFUnclampedDelayTime or kCGImagePropertyGIFDelayTime
        NSNumber *delayTimeUnclampedProp = gifProperties[(NSString*)kCGImagePropertyGIFUnclampedDelayTime];
        if(delayTimeUnclampedProp) {
            frameDuration = [delayTimeUnclampedProp floatValue];
        } else {
            NSNumber *delayTimeProp = gifProperties[(NSString*)kCGImagePropertyGIFDelayTime];
            if(delayTimeProp) {
                frameDuration = [delayTimeProp floatValue];
            }
        }

        // Make sure its not too small
        if (frameDuration < 0.011f){
            frameDuration = 0.100f;
        }

        [tempTimesArray addObject:[NSNumber numberWithFloat:frameDuration]];

        // Release
        CFRelease(cfFrameProperties);

        // Add frame to array of frames
        CGImageRef frame = CGImageSourceCreateImageAtIndex(src, i, nil);
        [framesArray addObject:(__bridge id)(frame)];

        // Compile total loop time
        time = time + frameDuration;
    }

    NSMutableArray *timesArray = [NSMutableArray array];
    float base = 0;
    for (NSNumber* duration in tempTimesArray){
        //duration = [NSNumber numberWithFloat:(duration.floatValue/time) + base];
        base = base + (duration.floatValue/time);
        [timesArray addObject:[NSNumber numberWithFloat:base]];
    }

    // Create animation
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];

    animation.duration = time;
    animation.repeatCount = HUGE_VALF;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.values = framesArray;
    animation.keyTimes = timesArray;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.calculationMode = kCAAnimationDiscrete;

    return animation;
}

要在视图层中添加图像,请编写以下代码

    CALayer *layer = [CALayer layer];
    layer.frame = self.preview_view.bounds;

   CAKeyframeAnimation *animation =  [self createGIFAnimation:[NSData dataWithContentsOfURL:url]];
    [layer addAnimation:animation forKey:@"contents"];
    ["YOUR VIEW".layer addSublayer:layer];

【讨论】:

  • 你有这方面的快速例子吗?
  • @yaali 我没有快速的例子。
  • 我按照上述步骤将图层添加到视图的子图层,但在我的屏幕上看不到 gif 图像:( 我仔细检查了动画的输出,我正在获取值。我能够获取 CAkeyframeAnimation 但无法在视图中呈现它。
【解决方案2】:

用你的图像视图试试这个。

CALayer *layer = [CALayer layer];
layer.backgroundColor = [[UIColor whiteColor] CGColor];

layer.contents = (id) [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]].CGImage;
NSURL *url = [[NSBundle mainBundle] URLForResource:@"02" withExtension:@"gif"];
[[self.view layer] addSublayer:layer];
[layer setNeedsDisplay];

[self.view addSubview: animateImageView];

【讨论】:

  • 通过添加这个东西,它可以成功运行。我想要的是添加图像作为视图层,这样我就可以合并两个层并制作一个公共视频
猜你喜欢
  • 1970-01-01
  • 2019-02-03
  • 2020-07-02
  • 2017-08-15
  • 2012-04-22
  • 1970-01-01
  • 2012-09-03
  • 2015-09-10
  • 1970-01-01
相关资源
最近更新 更多