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