1.圖片或者視頻在icloud上,選擇后返回phasset對(duì)象存在不為空,但是獲取本地路徑為nil。
在選擇資源時(shí),可以判斷本地資源是否可用,不可用代表在icloud的,需要下載,而在ZLPhotoBrowser中,是寫死了返回YES,邏輯被注釋掉了,我們將這個(gè)方法換成以下方法,這樣在選擇資源時(shí),就會(huì)判斷本地資源是否可用,并有相應(yīng)的提示或者下載
+ (BOOL)judgeAssetisInLocalAblum:(PHAsset *)asset
{
// return YES;
__block BOOL isInLocal = YES;
if (@available(iOS 10.0, *)) {
// https://stackoverflow.com/questions/31966571/check-given-phasset-is-icloud-asset
// 這個(gè)api雖然是9.0出的,但是9.0會(huì)全部返回NO,未知原因,暫時(shí)先改為10.0
NSArray *resourceArray = [PHAssetResource assetResourcesForAsset:asset];
for (id obj in resourceArray) {
BOOL result = [[obj valueForKey:@"locallyAvailable"] boolValue];
if (!result){
isInLocal = NO;
}
}
} else {
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = PHImageRequestOptionsResizeModeFast;
option.networkAccessAllowed = NO;
option.synchronous = YES;
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(100, 100) contentMode:PHImageContentModeAspectFit options:option resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
if ([[info objectForKey:PHImageResultIsInCloudKey] boolValue]) {
isInLocal = NO;
}
}];
}
return isInLocal;
}
2.該框架并沒(méi)有提供非混合選擇模式下(選的第一個(gè)資源是圖片,那視頻資源就不可選;選了視頻,圖片資源就不可選)對(duì)應(yīng)的圖片選擇上限和視頻選擇上限,比如選圖片能選9張,視頻只能選1個(gè)。
找到以下三個(gè)類,帶有以下注釋的if語(yǔ)句就是我修改或者添加的方法,注意添加的位置要和我保持一致,其他的不需要變化
ZLShowBigImgViewController.m
ZLThumbnailViewController.m
ZLPhotoActionSheet.m
// 最多選擇圖片數(shù)...
if...
// 最多選擇視頻數(shù)...
if...
這個(gè)是我為configuration添加的一個(gè)屬性,用來(lái)設(shè)置視頻選擇的最大上限
configuration.maxVideoSelectCount
ZLShowBigImgViewController覆蓋以下方法
- (void)navRightBtn_Click:(UIButton *)btn
{
ZLImageNavigationController *nav = (ZLImageNavigationController *)self.navigationController;
ZLPhotoConfiguration *configuration = nav.configuration;
ZLPhotoModel *model = self.models[_currentPage-1];
if (!btn.selected) {
//選中
[btn.layer addAnimation:GetBtnStatusChangedAnimation() forKey:nil];
// 最多選擇圖片數(shù)
if (configuration.allowMixSelect==NO && (model.type==ZLAssetMediaTypeGif||model.type==ZLAssetMediaTypeImage)) {
if (nav.arrSelectedModels.count >= configuration.maxSelectCount) {
ShowToastLong(GetLocalLanguageTextValue(ZLPhotoBrowserMaxSelectCountText), configuration.maxSelectCount);
return;
}
}
if (model.asset && ![ZLPhotoManager judgeAssetisInLocalAblum:model.asset]) {
ShowToastLong(@"%@", GetLocalLanguageTextValue(ZLPhotoBrowserLoadingText));
return;
}
if (model.type == ZLAssetMediaTypeVideo && GetDuration(model.duration) > configuration.maxVideoDuration) {
ShowToastLong(GetLocalLanguageTextValue(ZLPhotoBrowserMaxVideoDurationText), configuration.maxVideoDuration);
return;
}
// 最多選擇視頻數(shù)
if (configuration.allowMixSelect==NO && model.type==ZLAssetMediaTypeVideo) {
if (nav.arrSelectedModels.count >= configuration.maxVideoSelectCount) {
[MBProgressHUD showError:[NSString stringWithFormat:@"最多只能選擇%ld個(gè)視頻", configuration.maxVideoSelectCount]];
return;
}
}
model.selected = YES;
[nav.arrSelectedModels addObject:model];
if (self.arrSelPhotos) {
[self.arrSelPhotos addObject:_arrSelPhotosBackup[_currentPage-1]];
[_arrSelAssets addObject:_arrSelAssetsBackup[_currentPage-1]];
}
} else {
//移除
model.selected = NO;
for (ZLPhotoModel *m in nav.arrSelectedModels) {
if ([m.asset.localIdentifier isEqualToString:model.asset.localIdentifier] ||
[m.image isEqual:model.image] ||
[m.url.absoluteString isEqualToString:model.url.absoluteString]) {
[nav.arrSelectedModels removeObject:m];
break;
}
}
if (self.arrSelPhotos) {
for (PHAsset *asset in _arrSelAssets) {
if ([asset isEqual:_arrSelAssetsBackup[_currentPage-1]]) {
[_arrSelAssets removeObject:asset];
break;
}
}
[self.arrSelPhotos removeObject:_arrSelPhotosBackup[_currentPage-1]];
}
}
btn.selected = !btn.selected;
[self getPhotosBytes];
[self resetDontBtnState];
[self resetEditBtnState];
}
ZLThumbnailViewController覆蓋以下方法
- (BOOL)canAddModel:(ZLPhotoModel *)model
{
ZLImageNavigationController *nav = (ZLImageNavigationController *)self.navigationController;
ZLPhotoConfiguration *configuration =nav.configuration;
// 非混合選擇
ZLPhotoModel *smmm = nav.arrSelectedModels.firstObject;
// 最多選擇圖片數(shù)
if (configuration.allowMixSelect==NO && (smmm.type==ZLAssetMediaTypeGif||smmm.type==ZLAssetMediaTypeImage)) {
if (nav.arrSelectedModels.count >= configuration.maxSelectCount) {
ShowToastLong(GetLocalLanguageTextValue(ZLPhotoBrowserMaxSelectCountText), configuration.maxSelectCount);
return NO;
}
}
if (nav.arrSelectedModels.count > 0) {
ZLPhotoModel *sm = nav.arrSelectedModels.firstObject;
if (!configuration.allowMixSelect &&
((model.type < ZLAssetMediaTypeVideo && sm.type == ZLAssetMediaTypeVideo) || (model.type == ZLAssetMediaTypeVideo && sm.type < ZLAssetMediaTypeVideo))) {
ShowToastLong(@"%@", GetLocalLanguageTextValue(ZLPhotoBrowserCannotSelectVideo));
return NO;
}
}
if (![ZLPhotoManager judgeAssetisInLocalAblum:model.asset]) {
ShowToastLong(@"%@", GetLocalLanguageTextValue(ZLPhotoBrowseriCloudPhotoText));
return NO;
}
if (model.type == ZLAssetMediaTypeVideo && GetDuration(model.duration) > configuration.maxVideoDuration) {
ShowToastLong(GetLocalLanguageTextValue(ZLPhotoBrowserMaxVideoDurationText), configuration.maxVideoDuration);
return NO;
}
// 最多選擇視頻數(shù)
if (configuration.allowMixSelect==NO && smmm.type==ZLAssetMediaTypeVideo) {
if (nav.arrSelectedModels.count >= configuration.maxVideoSelectCount) {
[MBProgressHUD showError:[NSString stringWithFormat:@"最多只能選擇%ld個(gè)視頻", (long)configuration.maxVideoSelectCount]];
return NO;
}
}
return YES;
}
ZLPhotoActionSheet覆蓋以下方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ZLCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZLCollectionCell" forIndexPath:indexPath];
ZLPhotoModel *model = self.arrDataSources[indexPath.row];
@zl_weakify(self);
__weak typeof(cell) weakCell = cell;
cell.selectedBlock = ^(BOOL selected) {
@zl_strongify(self);
__strong typeof(weakCell) strongCell = weakCell;
if (!selected) {
//選中
// 非混合選擇
ZLPhotoModel *smmm = self.arrSelectedModels.firstObject;
// 最多選擇圖片數(shù)
if (self.configuration.allowMixSelect==NO && (smmm.type==ZLAssetMediaTypeGif||smmm.type==ZLAssetMediaTypeImage)) {
if (self.arrSelectedModels.count >= self.configuration.maxSelectCount) {
ShowToastLong(GetLocalLanguageTextValue(ZLPhotoBrowserMaxSelectCountText), self.configuration.maxSelectCount);
return;
}
}
if (self.arrSelectedModels.count > 0) {
ZLPhotoModel *sm = self.arrSelectedModels.firstObject;
if (!self.configuration.allowMixSelect &&
((model.type < ZLAssetMediaTypeVideo && sm.type == ZLAssetMediaTypeVideo) || (model.type == ZLAssetMediaTypeVideo && sm.type < ZLAssetMediaTypeVideo))) {
ShowToastLong(@"%@", GetLocalLanguageTextValue(ZLPhotoBrowserCannotSelectVideo));
return;
}
}
if (![ZLPhotoManager judgeAssetisInLocalAblum:model.asset]) {
ShowToastLong(@"%@", GetLocalLanguageTextValue(ZLPhotoBrowseriCloudPhotoText));
return;
}
if (model.type == ZLAssetMediaTypeVideo && GetDuration(model.duration) > self.configuration.maxVideoDuration) {
ShowToastLong(GetLocalLanguageTextValue(ZLPhotoBrowserMaxVideoDurationText), self.configuration.maxVideoDuration);
return;
}
// 最多選擇視頻數(shù)
if (self.configuration.allowMixSelect==NO && smmm.type==ZLAssetMediaTypeVideo) {
if (self.arrSelectedModels.count >= self.configuration.maxVideoSelectCount) {
[MBProgressHUD showError:[NSString stringWithFormat:@"最多只能選擇%ld個(gè)視頻", (long)self.configuration.maxVideoSelectCount]];
return;
}
}
if (![self shouldDirectEdit:model]) {
model.selected = YES;
[self.arrSelectedModels addObject:model];
strongCell.btnSelect.selected = YES;
}
} else {
strongCell.btnSelect.selected = NO;
model.selected = NO;
for (ZLPhotoModel *m in self.arrSelectedModels) {
if ([m.asset.localIdentifier isEqualToString:model.asset.localIdentifier]) {
[self.arrSelectedModels removeObject:m];
break;
}
}
}
if (self.configuration.showSelectedMask) {
strongCell.topView.hidden = !model.isSelected;
}
[self changeCancelBtnTitle];
};
cell.allSelectGif = self.configuration.allowSelectGif;
cell.allSelectLivePhoto = self.configuration.allowSelectLivePhoto;
cell.showSelectBtn = self.configuration.showSelectBtn;
cell.cornerRadio = self.configuration.cellCornerRadio;
cell.showMask = self.configuration.showSelectedMask;
cell.maskColor = self.configuration.selectedMaskColor;
cell.model = model;
return cell;
}