gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:54:14 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com