我用的 YYAnimatedImageView 方式加载动图,可以开始播放的时间,是否重复播放,从哪一帧开始播放等。简单好用。
个别老款手机,个别场景卡顿:
YYImage.init(named: "launchGif.gif")
这种方式添加的gif,个别情况,老手机会出现播放卡顿的情况。
解决办法:
用 UIImage.sd_tz_animatedGIF(with: imgDate)
替代就OK
直接上代码:
let path = Bundle.main.path(forResource: "launchGif", ofType: "gif")
var lastImg: UIImage = YYImage.init(named: "launchGif.gif") ?? UIImage.init() // 个别老手机卡顿
if let imgDate = NSData.init(contentsOfFile: path ?? "") as Data? {
lastImg = UIImage.sd_tz_animatedGIF(with: imgDate) // 用这种方式,能避免个别手机卡顿的bug
}
let videoGifImgV: YYAnimatedImageView = YYAnimatedImageView.init(image: lastImg)
videoGifImgV.autoPlayAnimatedImage = false
bgImageV.addSubview(videoGifImgV)
.....
循环播放本地视频 -- 类似Keep启动背景动画
Keep的启动视频15秒,竟然才800多KB
,GIF就很大,所有还是决定用mp4
!!!坑人的是播放视频竟然没循环播放的属性,需要添加监听方法,自己处理循环播放!!!
核心代码
#import <AVKit/AVKit.h>
/// 视频的方式
NSString *videoStr = @"login_iphone.mp4";
if (kIsIpad_XM) {
videoStr = @"login_ipad.mp4";
}
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:videoStr withExtension:nil]];
_player = [AVPlayer playerWithPlayerItem:item];
_player.volume = 0;
// 4.添加AVPlayerLayer
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
playerLayer.frame = CGRectMake(0, 0, kScreenWidth_XM, kScreenHeight_XM);
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:playerLayer];
[_player play];
UIView *maskV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth_XM, kScreenHeight_XM)];
maskV.backgroundColor = [UIColor blackColor];
maskV.alpha = 0.6;
[self.view addSubview:maskV];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
/// 视频循环播放
- (void)moviePlayDidEnd:(NSNotification*)notification {
AVPlayerItem *item = [notification object];
[item seekToTime:kCMTimeZero];
[self.player play];
}
- (void)dealloc {
XMLog("执行了,确保没循环引用");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}