ZLPhotoBrowser 使用埋坑 本地路徑返回為nil 視頻選擇最多上限數(shù)量

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

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

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒(méi)有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,679評(píng)論 1 32
  • 點(diǎn)擊查看原文 Web SDK 開發(fā)手冊(cè) SDK 概述 網(wǎng)易云信 SDK 為 Web 應(yīng)用提供一個(gè)完善的 IM 系統(tǒng)...
    layjoy閱讀 14,320評(píng)論 0 15
  • 文/Mission小可兒 被壓在五指之下的鼠標(biāo), 它,瘦瘦小小。 被一根長(zhǎng)線指牽的鼠標(biāo), 它,馬首是瞻。 一天, ...
    Mission小可兒閱讀 359評(píng)論 2 3
  • 早晨沒(méi)吃東西,送娃時(shí),老師說(shuō)這一陣娃很乖,也許是我的思路對(duì)了,也許是老師安慰我。我還是挺欣慰的。以后要繼續(xù)努力啊。...
    圈_圈_閱讀 178評(píng)論 0 0
  • 原因是因?yàn)閳D表數(shù)據(jù)會(huì)和之前的合并 解決辦法是setOption(option,true) 第二個(gè)參數(shù)設(shè)為true
    ctrlc_ctrlv閱讀 1,087評(píng)論 0 0

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