背景:
最近在做一個(gè)視頻拼圖的工具浑厚,涉及到相冊(cè)資源的選取股耽。這個(gè)不是項(xiàng)目重點(diǎn),所以直接應(yīng)用了第三方相冊(cè)選擇器钳幅。使用了一個(gè)功能挺齊全的相冊(cè)選擇器TZImagePickerController豺谈。具體特性這里不做贅述,感興趣的可以前往查看贡这。
github地址:https://github.com/banchichen/TZImagePickerController
---------------------------華麗的分割線-----------------------
這里遇到的問(wèn)題是當(dāng)設(shè)備占用存儲(chǔ)過(guò)高時(shí)茬末,而用戶又打開(kāi)了相冊(cè)同步iCloud,這時(shí)系統(tǒng)會(huì)刪除資源只保留縮略圖盖矫。而我們需要調(diào)用這些資源時(shí)丽惭,就會(huì)先從iCloud上下載資源。由于網(wǎng)速或資源大小的問(wèn)題辈双,會(huì)等待較長(zhǎng)時(shí)間责掏。TZImagePickerController處理了iCloud圖片的加載過(guò)程并伴有進(jìn)度交互,但對(duì)于iCloud視頻同步?jīng)]有處理湃望。同步視頻時(shí)一片黑屏换衬。
這里我需要做的是增加iCloud視頻同步時(shí)進(jìn)度交互。
TZImagePickerController原本的流程
1证芭、在TZPhotoPickerController的- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
方法中點(diǎn)擊選取了視頻后瞳浦、跳轉(zhuǎn)到TZVideoPlayerController
TZVideoPlayerController *videoPlayerVc = [[TZVideoPlayerController alloc] init];
videoPlayerVc.model = model;
[self.navigationController pushViewController:videoPlayerVc animated:YES];
2、在TZVideoPlayerController界面調(diào)用
- (void)getVideoWithAsset:(PHAsset *)asset completion:(void (^)(AVPlayerItem *, NSDictionary *))completion;
獲取視頻資源废士,獲取到之后再顯示UI(視頻和按鈕等)叫潦,在這期間界面一片空白。
[[TZImageManager manager] getVideoWithAsset:_model.asset completion:^(AVPlayerItem *playerItem, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
self->_player = [AVPlayer playerWithPlayerItem:playerItem];
self->_playerLayer = [AVPlayerLayer playerLayerWithPlayer:self->_player];
self->_playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self->_playerLayer];
[self addProgressObserver];
[self configPlayButton];
[self configBottomToolBar];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pausePlayerAndShowNaviBar) name:AVPlayerItemDidPlayToEndTimeNotification object:self->_player.currentItem];
});
}];
處理
這里先來(lái)做個(gè)簡(jiǎn)單的處理:在TZPhotoPickerController選擇了視頻時(shí)官硝,做一個(gè)預(yù)同步的處理矗蕊,并監(jiān)聽(tīng)同步進(jìn)度,界面彈出進(jìn)度提示氢架。完成后繼續(xù)原來(lái)的流程傻咖。這時(shí)候視頻已經(jīng)同步成功,這個(gè)時(shí)候在TZVideoPlayerController調(diào)用- (void)getVideoWithAsset:(PHAsset *)asset completion:(void (^)(AVPlayerItem *, NSDictionary *))completion
獲取視頻資源可以直接獲取到岖研。預(yù)同步的代碼如下卿操。
在TZPhotoPickerController的- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
方法中
//預(yù)獲取
ProgressBarShowLoading([NSBundle tz_localizedStringForKey:@"Loading"]);
PHVideoRequestOptions * option = [[PHVideoRequestOptions alloc] init];
option.networkAccessAllowed = YES;
option.progressHandler = ^(double progress, NSError * _Nullable error, BOOL * _Nonnull stop, NSDictionary * _Nullable info) {
NSLog(@"下載進(jìn)度:%f",progress);
NSString * currentPrecentage = [NSString stringWithFormat:@"%.2f%%",progress*100];
ProgressBarUpdateLoading([NSBundle tz_localizedStringForKey:@"Download from iCloud"], currentPrecentage);
};
[[PHImageManager defaultManager] requestAVAssetForVideo:model.asset options:option resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
dispatch_async(dispatch_get_main_queue(), ^{
ProgressBarDismissLoading(@"");
TZVideoPlayerController *videoPlayerVc = [[TZVideoPlayerController alloc] init];
videoPlayerVc.model = model;
[self.navigationController pushViewController:videoPlayerVc animated:YES];
});
}];
總結(jié)
因?yàn)槲疫@里用到的是單選,這只是一個(gè)簡(jiǎn)單的處理缎玫。后期可以改為選擇某個(gè)資源時(shí)在這個(gè)資源上顯示同步進(jìn)度硬纤,可以適配多選的情況解滓。
歡迎提出更好的解決方法赃磨。
這里再次感謝非常感謝@半遲塵提供的如此強(qiáng)大的工具TZImagePickerController