2016-09-30 89 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

對不起,但它不起作用 –