2016-09-30 132 views
1

我正在开发录制视频的项目。
录制完成后,我使用AVPlayer添加图层来播放视频。
现在我想要一个gif图像作为添加层到该视图。如何将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]; 

请帮我一下吧

谢谢

+0

我认为你必须使用startAnimating图像来显示其图像动画。 – mn1

+0

但是,开始动画与UIimageview一起使用。我使用CALayer在视图上添加图像作为图层 –

+0

如果您将layer.contents等同于包含gif的UIImageView并且您为gif制作动画,该怎么办? – mn1

回答

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

你有这样一个快速的例子吗? – yaali

+0

@yaali我没有快速的例子。 –

+0

我遵循上面的过程,并添加了图层到我的视图的子图层,但无法看到我的屏幕中的GIF图像:(我双重检查动画的输出,我得到的价值。我能够得到CAkeyframeAnimation但不能在视图中呈现它。 – yaali

0

用您的图片视图试一次。

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

通过添加这个东西,它成功地工作。我想要的是添加图像作为一个视图,所以我可以合并这两个图层,并制作一个共同的视频 –

+0

检查更新的答案 – Nagarjun

+0

对不起,但它不起作用 –