控制gif只播一次

1,388 阅读1分钟

gif 使用sd_animatedGIFWithData加载之后会循环播放,在这里记录一下gif只播一次的方法

    UIImageView *gifImageView = [[UIImageView alloc] init];

    NSBundle *bundle = [NSBundle mainBundle];

    NSString  *filePath = [bundle pathForResource:@"map" ofType:@"gif"];

    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];

    UIImage *imageGif = [UIImage sd_animatedGIFWithData:imageData];

    gifImageView.animationImages = imageGif.images;

    gifImageView.animationDuration = 2.0f;

    gifImageView.animationRepeatCount = 1;

    self.lastImage = imageGif.images.lastObject;

    [gifImageView startAnimating];

    [NSObject cancelPreviousPerformRequestsWithTarget:self];

    [self performSelector:@selector(animationDidFinish) withObject:self afterDelay:gifImageView.animationRepeatCount * gifImageView.animationDuration];

//    gifImageView.image = imageGif;

    [self addSubview:gifImageView];

    self.gifImageView = gifImageView;

    [gifImageView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.offset(0);

        make.bottom.offset(0);

        make.width.offset([UIScreen mainScreen].bounds.size.width);

        make.height.offset([UIScreen mainScreen].bounds.size.width * 0.75);

    }];
    
- (void)animationDidFinish {

    self.gifImageView.image = **self**.lastImage;

}