iOS 頭像選擇控件JCChoosePhoneManager

通常項目都有用戶中心選取頭像功能,為了可以減少開發(fā)量并組件化,書寫了該控件。大家可以通過pod直接導(dǎo)入?;蛘咄ㄟ^git地址下載測試demo進(jìn)行相關(guān)調(diào)試。
pod:pod 'JCChoosePhoneManager'
傳送門: git地址

1.架構(gòu)組成及部分功能代碼介紹

  • JCCustomChooseProtocol 控件協(xié)議
  • JCCustomChooseView 默認(rèn)控件(可自制控件)
  • JCChoosePhoneManager 管理工具
  • JCVPImageCropperViewController 圖片管理類
  • Aspects 第三方(鉤子類庫)
JCCustomChooseProtocol 定義了自定義控件的主要功能協(xié)議,控件必須遵守協(xié)議并實現(xiàn)功能。用戶只需要管理實現(xiàn)這些協(xié)議即可快速集成自己的控件選取框。
// 選擇拍照
- (void)chooseCamera;

// 選擇相冊
- (void)choosePhoto;

// 取消選擇
- (void)cencleChoose;

// 展示
- (void)show;

// 隱藏
- (void)dissmiss;

JCCustomChooseView 作者定義的一個defule控件,里面遵守JCCustomChooseProtocol并實現(xiàn)。用戶可以把聚焦的重點放在UI的定制上。
JCChoosePhoneManager 主要功能的管理者,邏輯的處理方,功能枚舉可監(jiān)聽用戶行為。
// 選擇相冊類型設(shè)置枚舉
typedef enum : NSUInteger {
    // 無優(yōu)化圖片并不選擇使用VPImage
    JCChoosePhoneNoOptimizeNoVPImageType = 0,
    // 無優(yōu)化圖片并選擇使用VPImage
    JCChoosePhoneNoOptimizeHaveVPImageType,
    // 優(yōu)化圖片并不選擇使用VPImage
    JCChoosePhoneHaveOptimizeNoVPImageType,
    // 優(yōu)化圖片并選擇使用VPImage
    JCChoosePhoneHaveOptimizeHaveVPImageType
} JCChoosePhoneManagerSetType;

// 彈出框之后拍照行為
typedef enum : NSUInteger {
    // 選擇照相機(jī)
    JCChooseGetPhotoCameraType,
    // 選擇相冊
    JCChooseGetPhotoPhotosType,
    // 取消選擇
    JCChooseGetPhotoCancelType
} JCChooseGetPhotoType;

// 選取視圖
typedef enum : NSUInteger {
    // 系統(tǒng)選取框
    JCChooseSystemViewType,
    // 自定義選取框
    JCChooseCustomViewType
} JCChooseViewType;

實現(xiàn)以下方法傳入?yún)f(xié)議控件可定制自己的試圖控件。如果不實現(xiàn),組件會以系統(tǒng)默認(rèn)彈窗的形式進(jìn)行實現(xiàn)
/**
 *  @author XiaoJianJie, 16-06-30 17:06:00
 *
 *  @brief 設(shè)置選取視圖
 *
 *  @param customChooseView 自定義的選取視圖
 */
- (void)configCustomChooseView:(UIView <JCCustomChooseProtocol> *)customChooseView superView:(UIView *)superView;

可根據(jù)業(yè)務(wù)選擇不同的功能模式,調(diào)用該方法會彈出自定義控件
/**
 *  @author XiaoJianJie, 16-06-30 17:06:00
 *
 *  @brief 開始使用選取圖片管理器
 *
 *  @param currentVC 使用的控制器
 *  @param type      設(shè)置的類型
 */
- (void)starToChooseThePhoneWithCurrentVC:(UIViewController *)currentVC
                                     type:(JCChoosePhoneManagerSetType)type;

下列兩個方法可根據(jù)自己的需要進(jìn)行調(diào)用,不會和協(xié)議控件進(jìn)行耦合??芍苯佑|發(fā)系統(tǒng)功能(拍照,相冊)。
/**
 *  @author XiaoJianJie, 16-06-30 18:06:21
 *
 *  @brief 拍照獲取圖片
 *
 *  @param currentVC 使用的控制器
 */
- (void)getPhoneForCameraWithCurrentVC:(UIViewController *)currentVC;

/**
 *  @author XiaoJianJie, 16-06-30 18:06:21
 *
 *  @brief 相冊獲取圖片
 *
 *  @param currentVC 使用的控制器
 */
- (void)getPhoneForPhotosWithCurrentVC:(UIViewController *)currentVC;

JCVPImageCropperViewController 使用的是VPImageCropperViewController為了避免命名空間問題添加了前綴
Aspects 第三方框架,主要是為了實現(xiàn)協(xié)議控件與功能之間的綁定與橋接。
- (void)setCustomChooseHook {
    
    __block JCChooseGetPhotoType chooseGetPhotoType = JCChooseGetPhotoCancelType;
    __weak typeof(self)  weakSelf   = self;
    if (!NSClassFromString(@"XCTestCase")) {
        [self.customChooseView aspect_hookSelector:NSSelectorFromString(@"chooseCamera") withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> info) {
            chooseGetPhotoType = JCChooseGetPhotoCameraType;
            [weakSelf paiZhao];
            [weakSelf.customChooseView dissmiss];
            if (weakSelf.chooseGetPhotoWayOfAct) {
                weakSelf.chooseGetPhotoWayOfAct(chooseGetPhotoType);
            }
        } error:NULL];

        [self.customChooseView aspect_hookSelector:NSSelectorFromString(@"choosePhoto") withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> info) {
            chooseGetPhotoType = JCChooseGetPhotoPhotosType;
            [weakSelf xiangCe];
            [weakSelf.customChooseView dissmiss];
            if (weakSelf.chooseGetPhotoWayOfAct) {
                weakSelf.chooseGetPhotoWayOfAct(chooseGetPhotoType);
            }
        } error:NULL];

        [self.customChooseView aspect_hookSelector:NSSelectorFromString(@"cencleChoose") withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> info) {
            [weakSelf.customChooseView dissmiss];
            if (weakSelf.chooseGetPhotoWayOfAct) {
                weakSelf.chooseGetPhotoWayOfAct(chooseGetPhotoType);
            }
        } error:NULL];
    }
}

2.測試項目詳解

測試項目書寫了4種方式的選取UI進(jìn)行測試。并使用了默認(rèn)的協(xié)議控件。
1.設(shè)置自定義選擇框視圖 (可不設(shè)置即使用系統(tǒng)彈窗展示)
[[JCChoosePhoneManager showChoosePhoneManager] configCustomChooseView:self.customChooseView superView:self.view];

2.根據(jù)選取的類型彈出頭像選取控件
[[JCChoosePhoneManager showChoosePhoneManager] starToChooseThePhoneWithCurrentVC:weakSelf type:choosePhoneManagerSetType];

3.根據(jù)單例管理器監(jiān)聽用戶行為
__weak typeof(self)  weakSelf   = self;
    [JCChoosePhoneManager showChoosePhoneManager].chooseGetPhotoWayOfAct = ^(JCChooseGetPhotoType type){
        
        switch (type) {
            case JCChooseGetPhotoCameraType:
            {
                NSLog(@"選擇照相機(jī)");
            }
                break;
            case JCChooseGetPhotoPhotosType:
            {
                NSLog(@"選擇相冊");
            }
                break;
            case JCChooseGetPhotoCancelType:
            {
                NSLog(@"取消選擇");
            }
                break;
            default:
                break;
        }
    };
    
    [JCChoosePhoneManager showChoosePhoneManager].chooseImage = ^(UIImage *chooseImage){
        NSLog(@"選擇的圖片為 = %@",chooseImage);
        weakSelf.choosePhoneView.chooseImageView.image = chooseImage;
    };
    
    [JCChoosePhoneManager showChoosePhoneManager].cancelChooseImage = ^(){
        NSLog(@"取消選擇圖片了");
    };

3.結(jié)語

該控件可以快速集成頭像選取功能,并可單獨使用,無耦合。大家在使用中有任何問題可聯(lián)系我,如果覺得還不錯記得給一顆星哦。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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