iOS頭像編輯上傳功能

iOS的相冊(cè)選擇和拍照就自帶有照片的編輯功能,在這里我就利用了這一功能對(duì)頭像編輯做了一個(gè)簡(jiǎn)單的封裝。

1.首先創(chuàng)建一個(gè)單列以及定義一個(gè)block來回調(diào)選擇之后的照片。

@interface UpLoadUserpicTool ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>


@property (nonatomic, strong)UIViewController *viewController;

@property (nonatomic, copy)FinishSelectImageBlcok imageBlock;

@end
+ (UpLoadUserpicTool *)shareManager
{
    static UpLoadUserpicTool *managerInstance = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        managerInstance = [[self alloc] init];
    });
    return managerInstance;
}

2.創(chuàng)建一個(gè)方法,選擇上傳照片的方式,以及初始化回調(diào)的block.這里用的是UIAlertController來實(shí)現(xiàn)照片來源的選擇。然后關(guān)鍵的一點(diǎn)就是UIImagePickerController有一個(gè)屬性allowsEditing(是否允許編輯),設(shè)置為YES。

- (void)selectUserpicSourceWithViewController:(UIViewController *)viewController FinishSelectImageBlcok:(FinishSelectImageBlcok)finishSelectImageBlock{
    
    if (viewController) {
        self.viewController = viewController;
    }
    if (finishSelectImageBlock) {
        self.imageBlock = finishSelectImageBlock;
    }
    UIAlertController *alertCol = [UIAlertController alertControllerWithTitle:@"請(qǐng)選擇頭像來源" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])///<檢測(cè)該設(shè)備是否支持拍攝
        {
            //                    [Tools showAlertView:@"sorry, 該設(shè)備不支持拍攝"];///<顯示提示不支持
            
            return;
        }
        UIImagePickerController* picker = [[UIImagePickerController alloc]init];///<圖片選擇控制器創(chuàng)建
        
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;///<設(shè)置數(shù)據(jù)來源為拍照
        picker.allowsEditing = YES;
        picker.delegate = self;///<代理設(shè)置
        
        [self.viewController presentViewController:picker animated:YES completion:nil];///<推出視圖控制器
        
        
    }];
     [alertCol addAction:action];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相冊(cè)" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        UIImagePickerController* picker = [[UIImagePickerController alloc]init];///<圖片選擇控制器創(chuàng)建
        
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;///<設(shè)置數(shù)據(jù)來源為相冊(cè)
        //允許選擇照片之后可編輯
        picker.allowsEditing = YES;
        picker.delegate = self;///<代理設(shè)置
        
        [viewController presentViewController:picker animated:YES completion:nil];///<推出視圖控制器
        
    }];
    [alertCol addAction:action2];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
       
    
        
    }];
    [alertCol addAction:cancelAction];
   
    [viewController presentViewController:alertCol animated:YES completion:^{
        
        
    }];
  
}

3.編輯完成之后照片的獲取,這里info中獲取的照片要選擇UIImagePickerControllerEditedImage類型的,這個(gè)是編輯之后的照片。

#pragma mark - 相冊(cè)/相機(jī)回調(diào)  顯示所有的照片,或者拍照選取的照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = nil;
    //獲取編輯之后的圖片
    image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (self.imageBlock) {
        
        self.imageBlock(image);
    }
    
    [self.viewController dismissViewControllerAnimated:YES completion:nil];
    
}


//  取消選擇 返回當(dāng)前試圖
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

利用自帶的實(shí)現(xiàn)頭像編輯就是這么簡(jiǎn)單,但是如果你要改變編輯框的形狀或者是大小的話,這種方法是不可以的,需要自己去自定義了。

最后....

為了偷懶將dome和UIAlertController寫在了一起。
這是關(guān)于AlertCntroller文章:
http://www.itdecent.cn/p/b89c3e96aba6
dome下載地址
[下載鏈接]https://git.oschina.net/ioszxh/iOS-UIAlertController

最后編輯于
?著作權(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)容

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