Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在视图中将GIf图像添加为图层?_Ios_Objective C_Calayer - Fatal编程技术网

Ios 如何在视图中将GIf图像添加为图层?

Ios 如何在视图中将GIf图像添加为图层?,ios,objective-c,calayer,Ios,Objective C,Calayer,我正在处理一个录制视频的项目。录制完成后,我将使用AVPlayer播放视频,并将图层添加到视图中。现在我想要一个gif图像作为该视图的图层 我成功地将gif图像添加为图层,但图像未设置动画。它就像一个静态图像。我使用GIF图像 我的代码在下面 self.avPlayer = [AVPlayer playerWithURL:self.urlstring]; self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; AVPlayerL

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

我成功地将gif图像添加为图层,但图像未设置动画。它就像一个静态图像。
我使用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];
请帮帮我


谢谢你

用你的图像视图试试这个

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

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

我相信您必须使用startAnimating来显示图像动画。但是startAnimating与UIimageview一起使用。我使用CALayer将图像添加为视图上的图层,如果将layer.contents设置为包含gif的UIImageView,并设置gif动画,结果仍然相同。图像不是动画通过添加这个东西,它是成功的工作。我想要的是将图像添加为视图的一个层,这样在我可以合并两个层并制作一个普通视频后,您有关于此的swift示例吗?@yaali我没有swift示例。我按照上述步骤将该层添加到视图的子层,但在我的屏幕中看不到gif图像:(我仔细检查了动画的输出,我得到了值。我能够得到CAkeyframeAnimation,但无法在视图中渲染它。
    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];