iOS的相冊選擇和拍照就自帶有照片的編輯功能,在這里我就利用了這一功能對頭像編輯做了一個簡單的封裝。
1.首先創建一個單列以及定義一個block來回調選擇之后的照片。
@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.創建一個方法,選擇上傳照片的方式,以及初始化回調的block.這里用的是UIAlertController來實現照片來源的選擇。然后關鍵的一點就是UIImagePickerController有一個屬性allowsEditing(是否允許編輯),設置為YES。
- (void)selectUserpicSourceWithViewController:(UIViewController *)viewController FinishSelectImageBlcok:(FinishSelectImageBlcok)finishSelectImageBlock{
if (viewController) {
self.viewController = viewController;
}
if (finishSelectImageBlock) {
self.imageBlock = finishSelectImageBlock;
}
UIAlertController *alertCol = [UIAlertController alertControllerWithTitle:@"請選擇頭像來源" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])///<檢測該設備是否支持拍攝
{
// [Tools showAlertView:@"sorry, 該設備不支持拍攝"];///<顯示提示不支持
return;
}
UIImagePickerController* picker = [[UIImagePickerController alloc]init];///<圖片選擇控制器創建
picker.sourceType = UIImagePickerControllerSourceTypeCamera;///<設置數據來源為拍照
picker.allowsEditing = YES;
picker.delegate = self;///<代理設置
[self.viewController presentViewController:picker animated:YES completion:nil];///<推出視圖控制器
}];
[alertCol addAction:action];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController* picker = [[UIImagePickerController alloc]init];///<圖片選擇控制器創建
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;///<設置數據來源為相冊
//允許選擇照片之后可編輯
picker.allowsEditing = YES;
picker.delegate = self;///<代理設置
[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類型的,這個是編輯之后的照片。
#pragma mark - 相冊/相機回調 顯示所有的照片,或者拍照選取的照片
- (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];
}
// 取消選擇 返回當前試圖
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
利用自帶的實現頭像編輯就是這么簡單,但是如果你要改變編輯框的形狀或者是大小的話,這種方法是不可以的,需要自己去自定義了。
最后....
為了偷懶將dome和UIAlertController寫在了一起。
這是關于AlertCntroller文章:
http://www.lxweimin.com/p/b89c3e96aba6
dome下載地址
[下載鏈接]https://git.oschina.net/ioszxh/iOS-UIAlertController