123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- //
- // HeadPhotoSelectView.m
- // MIT_Endorsement
- //
- // Created by 翟玉磊 on 2017/9/25.
- // Copyright © 2017年 翟玉磊. All rights reserved.
- //
- #import "HeadPhotoSelectView.h"
- @interface HeadPhotoSelectView ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- {
-
- BOOL _isEditing; //是否开启裁剪 默认开启
- BOOL _isOpenHead; //是否开启头像选择(会进行压缩)默认开启
- }
- @property (nonatomic, strong) UIAlertController *actionSheetController;
- @property (nonatomic, strong) UIViewController *tempController;
- @end
- @implementation HeadPhotoSelectView
- + (instancetype)shared {
-
- static HeadPhotoSelectView *sharedManager;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedManager = [[HeadPhotoSelectView alloc] initWithFrame:[UIScreen mainScreen].bounds];
- });
- return sharedManager;
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
-
- self.backgroundColor = [UIColor clearColor];
- [self buildView];
- }
- return self;
- }
- - (void)buildView {
-
- self.actionSheetController = [UIAlertController alertControllerWithTitle:kLocalizedString(@"添加图片") message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- UIAlertAction *photoAction = [UIAlertAction actionWithTitle:kLocalizedString(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- NSLog(@"打开相机");
- [self showCamera];
- }];
- UIAlertAction *takeAction = [UIAlertAction actionWithTitle:kLocalizedString(@"从相册选择") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- NSLog(@"打开相册");
- [self openPhotoAlbum];
- }];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:kLocalizedString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
- [self removeFromSuperview];
- }];
-
- [self.actionSheetController addAction:photoAction];
- [self.actionSheetController addAction:takeAction];
- [self.actionSheetController addAction:cancelAction];
- }
- - (void)showWithDelegate:(UIViewController *)delegate {
-
- _isEditing = YES;
- _isOpenHead = YES;
- _tempController = delegate;
-
- [_tempController presentViewController:self.actionSheetController animated:YES completion:nil];
- }
- - (void)dismiss {
-
- }
- - (void)setupAllowsEditing:(BOOL)isEditing {
-
- _isEditing = isEditing;
- }
- - (void)openHeadSelect:(BOOL)isOpenHead {
-
- _isOpenHead = isOpenHead;
- }
- #pragma mark - 调用相机设备和图片库
- //打开图库
- - (void)openPhotoAlbum{
- if ([SystemRightObject isAlbumRight]) {
- UIImagePickerController *controller = [[UIImagePickerController alloc] init];
- controller.delegate = self;
- controller.allowsEditing = _isEditing;
- controller.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
-
- [self.tempController presentViewController:controller animated:YES completion:nil];
- }
- }
- - (void)showCamera{
- if ([SystemRightObject isCameraRightPopup:YES]) {
- UIImagePickerController *controller = [[UIImagePickerController alloc] init];
- controller.delegate = self;
- controller.allowsEditing = _isEditing;
- controller.sourceType = UIImagePickerControllerSourceTypeCamera;
-
- [self.tempController presentViewController:controller animated:YES completion:nil];
- }
- }
- //对图片进行裁剪
- - (NSData *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize setType:(int)type;
- {
- UIGraphicsBeginImageContext(newSize);
- [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
- UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- if(type==1)
- return UIImageJPEGRepresentation(newImage, 0.1);
- return UIImagePNGRepresentation(newImage);
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
- if ([picker isKindOfClass:[UIImagePickerController class]]) {
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- }
- -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
- NSData* imageData=nil;
- UIImage *image = nil;
- if (_isEditing) {
- image = [info valueForKey:UIImagePickerControllerEditedImage];
- }else {
- image = [info valueForKey:UIImagePickerControllerOriginalImage];
-
- //解决手机拍照不裁剪旋转90度的问题
- image = [image fixOrientation];
- }
-
- //判断图片是不是png格式的文件
- CGSize size;
- size=CGSizeMake(image.size.width/4, image.size.height/4);
- if (UIImagePNGRepresentation(image)) {
- //返回为png图像。
- if (_isOpenHead) {
- imageData = [self imageWithImage:image scaledToSize:size setType:0];
- }else {
- imageData = UIImagePNGRepresentation(image);
- }
- }else {
- //返回为JPEG图像。
- if (_isOpenHead) {
- imageData=[self imageWithImage:image scaledToSize:size setType:1];
- }else {
- imageData = UIImageJPEGRepresentation(image, 1.0);
- }
- }
-
- [picker dismissViewControllerAnimated:YES completion:^{
- UIImage* endImage = [UIImage imageWithData:imageData];
- if (self.didSelectedCompletionCallback) {
- self.didSelectedCompletionCallback(endImage);
- }
- }];
- }
- - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
- NSString *message = @"";
- if (!error) {
- message = @"成功保存到相册";
- }else
- {
- message = [error description];
- }
- NSLog(@"message is %@",message);
- }
- @end
|