美文网首页iOS开发高级
ZLPhotoBrowser 使用埋坑 本地路径返回为nil 视

ZLPhotoBrowser 使用埋坑 本地路径返回为nil 视

作者: 帅气的阿斌 | 来源:发表于2019-08-14 10:34 被阅读0次

    1.图片或者视频在icloud上,选择后返回phasset对象存在不为空,但是获取本地路径为nil。

    在选择资源时,可以判断本地资源是否可用,不可用代表在icloud的,需要下载,而在ZLPhotoBrowser中,是写死了返回YES,逻辑被注释掉了,我们将这个方法换成以下方法,这样在选择资源时,就会判断本地资源是否可用,并有相应的提示或者下载

    + (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
                // 这个api虽然是9.0出的,但是9.0会全部返回NO,未知原因,暂时先改为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.该框架并没有提供非混合选择模式下(选的第一个资源是图片,那视频资源就不可选;选了视频,图片资源就不可选)对应的图片选择上限和视频选择上限,比如选图片能选9张,视频只能选1个。
    找到以下三个类,带有以下注释的if语句就是我修改或者添加的方法,注意添加的位置要和我保持一致,其他的不需要变化

    
    ZLShowBigImgViewController.m
    ZLThumbnailViewController.m
    ZLPhotoActionSheet.m
    
    // 最多选择图片数...
    if...
    // 最多选择视频数...
    if...
    

    这个是我为configuration添加的一个属性,用来设置视频选择的最大上限

    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];
            
            // 最多选择图片数
            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;
            }
            
            // 最多选择视频数
            if (configuration.allowMixSelect==NO && model.type==ZLAssetMediaTypeVideo) {
                if (nav.arrSelectedModels.count >= configuration.maxVideoSelectCount) {
                    [MBProgressHUD showError:[NSString stringWithFormat:@"最多只能选择%ld个视频", 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;
        
        // 最多选择图片数
        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;
        }
        
        // 最多选择视频数
        if (configuration.allowMixSelect==NO && smmm.type==ZLAssetMediaTypeVideo) {
            if (nav.arrSelectedModels.count >= configuration.maxVideoSelectCount) {
                [MBProgressHUD showError:[NSString stringWithFormat:@"最多只能选择%ld个视频", (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;
                
                // 最多选择图片数
                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;
                }
                
                // 最多选择视频数
                if (self.configuration.allowMixSelect==NO && smmm.type==ZLAssetMediaTypeVideo) {
                    if (self.arrSelectedModels.count >= self.configuration.maxVideoSelectCount) {
                        [MBProgressHUD showError:[NSString stringWithFormat:@"最多只能选择%ld个视频", (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;
    }
    

    相关文章

      网友评论

        本文标题:ZLPhotoBrowser 使用埋坑 本地路径返回为nil 视

        本文链接:https://www.haomeiwen.com/subject/iipwjctx.html