SceneKit 播放Gif

最近在研究SceneKit播放Gif,本來打算用UIImageView來播放的,卻發(fā)現(xiàn)不能播放,在播放是會有一層白色的底色,不知道怎么去除。

再仔細(xì)看看API
image

不支持UIView,支持image label和CALayer等,所以就用layer來做動畫了。

借鑒網(wǎng)上的方法將gifData數(shù)據(jù)進(jìn)行分析,獲取到每一幀的圖片,和總的時間

CGImageSourceRef source =

CGImageSourceCreateWithData(CFDataRef data, NULL)

size_t count = CGImageSourceGetCount(source);

float allTime = 0;

//存放所有圖片

NSMutableArray *imageArray = [[NSMutableArray alloc] init];

NSMutableArray *timeArray = [[NSMutableArray alloc] init];

for (size_t i=0; i<count; i++) {

    CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);

    [imageArray addObject:(__bridge UIImage *)imageRef];

    CGImageRelease(imageRef);

    NSDictionary * info = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, i, NULL);

    NSDictionary *timeDic = [info objectForKey:(__bridge NSString*)kCGImagePropertyGIFDictionary];

    CGFloat time = [[timeDic objectForKey:(__bridge NSString*)kCGImagePropertyGIFDelayTime]floatValue];

    allTime += time;

    [timeArray addObject:[NSNumber numberWithFloat:time]];

}

利用

CAKeyframeAnimation

這個類做動畫

添加動畫

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];

    NSMutableArray *times = [[NSMutableArray alloc]init];

    float currentTime = 0;

    //設(shè)置每一幀的時間占比

    for (int i=0; i<imageArray.count; i++) {

        [times addObject:[NSNumber numberWithFloat:currentTime/totalTime]];

        currentTime += [timeArray[i] floatValue];

    }

    [animation setKeyTimes:times];

    [animation setValues:imageArray];

[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];

    //設(shè)置循環(huán)

    animation.repeatCount = MAXFLOAT;

    //設(shè)置播放總時長

    animation.duration = totalTime;

    animation.removedOnCompletion = NO;//動畫結(jié)束了禁止刪除

    animation.fillMode = kCAFillModeForwards;//停在動畫結(jié)束處 

    //Layer層添加

    [self addAnimation:animation forKey:@"gifAnimation"];

在外部的調(diào)用方法:

YXGifLayer *gifLayer = [YXGifLayer layer];

gifLayer.bounds = CGRectMake(0,0,[_wdgetsModel.width integerValue], [_wdgetsModel.height integerValue]);

gifLayer.position = CGPointMake([_wdgetsModel.width integerValue]/2, [_wdgetsModel.height integerValue]/2);

[gifLayer setGifData:data];

SCNPlane *nodeGifPlane = [SCNPlane planeWithWidth:[_wdgetsModel.width integerValue]/100.0 height:[_wdgetsModel.height integerValue]/100.0];

nodeGifPlane.firstMaterial.diffuse.contents = gifLayer;

self.geometry = nodeGifPlane;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容