ios - 如何将 GIf 图像添加为 View 图层?

标签 ios objective-c calayer

我正在做一个录制视频的项目。
录制完成后,我使用 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];

请帮帮我

谢谢

最佳答案

我已经成功了。我将我的 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;
}

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

    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];

关于ios - 如何将 GIf 图像添加为 View 图层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39785119/

相关文章:

ios - 使用 CAShapeLayer 和 CAGradientLayer 提高渐变边框的性能

ios - 使用自动布局自定义 UITableViewCell 的 subview - 没有 xibs,全部在代码中

iphone - 仍然对 iPhone 4 的新分辨率感到困惑

ios - 在 iOS 中将一个图像覆盖在另一个图像上

ios - PhoneGap 2.9.0 不工作构建错误

ios - 在触摸事件上识别 CALayer(将功能代码移植到 Swift)

ios - 核心数据 : using undefined transient attribute

ios - UIViewController 没有自行解除分配

objective-c - 用[x] [y]访问我的malloc二维数组

cocoa - 使用 CALayer 作为其他 NSView 的背景