HeadPhotoSelectView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // HeadPhotoSelectView.m
  3. // MIT_Endorsement
  4. //
  5. // Created by 翟玉磊 on 2017/9/25.
  6. // Copyright © 2017年 翟玉磊. All rights reserved.
  7. //
  8. #import "HeadPhotoSelectView.h"
  9. @interface HeadPhotoSelectView ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
  10. {
  11. BOOL _isEditing; //是否开启裁剪 默认开启
  12. BOOL _isOpenHead; //是否开启头像选择(会进行压缩)默认开启
  13. }
  14. @property (nonatomic, strong) UIAlertController *actionSheetController;
  15. @property (nonatomic, strong) UIViewController *tempController;
  16. @end
  17. @implementation HeadPhotoSelectView
  18. + (instancetype)shared {
  19. static HeadPhotoSelectView *sharedManager;
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. sharedManager = [[HeadPhotoSelectView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  23. });
  24. return sharedManager;
  25. }
  26. - (instancetype)initWithFrame:(CGRect)frame {
  27. if (self = [super initWithFrame:frame]) {
  28. self.backgroundColor = [UIColor clearColor];
  29. [self buildView];
  30. }
  31. return self;
  32. }
  33. - (void)buildView {
  34. self.actionSheetController = [UIAlertController alertControllerWithTitle:kLocalizedString(@"添加图片") message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  35. UIAlertAction *photoAction = [UIAlertAction actionWithTitle:kLocalizedString(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  36. NSLog(@"打开相机");
  37. [self showCamera];
  38. }];
  39. UIAlertAction *takeAction = [UIAlertAction actionWithTitle:kLocalizedString(@"从相册选择") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  40. NSLog(@"打开相册");
  41. [self openPhotoAlbum];
  42. }];
  43. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:kLocalizedString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  44. [self removeFromSuperview];
  45. }];
  46. [self.actionSheetController addAction:photoAction];
  47. [self.actionSheetController addAction:takeAction];
  48. [self.actionSheetController addAction:cancelAction];
  49. }
  50. - (void)showWithDelegate:(UIViewController *)delegate {
  51. _isEditing = YES;
  52. _isOpenHead = YES;
  53. _tempController = delegate;
  54. [_tempController presentViewController:self.actionSheetController animated:YES completion:nil];
  55. }
  56. - (void)dismiss {
  57. }
  58. - (void)setupAllowsEditing:(BOOL)isEditing {
  59. _isEditing = isEditing;
  60. }
  61. - (void)openHeadSelect:(BOOL)isOpenHead {
  62. _isOpenHead = isOpenHead;
  63. }
  64. #pragma mark - 调用相机设备和图片库
  65. //打开图库
  66. - (void)openPhotoAlbum{
  67. if ([SystemRightObject isAlbumRight]) {
  68. UIImagePickerController *controller = [[UIImagePickerController alloc] init];
  69. controller.delegate = self;
  70. controller.allowsEditing = _isEditing;
  71. controller.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
  72. [self.tempController presentViewController:controller animated:YES completion:nil];
  73. }
  74. }
  75. - (void)showCamera{
  76. if ([SystemRightObject isCameraRightPopup:YES]) {
  77. UIImagePickerController *controller = [[UIImagePickerController alloc] init];
  78. controller.delegate = self;
  79. controller.allowsEditing = _isEditing;
  80. controller.sourceType = UIImagePickerControllerSourceTypeCamera;
  81. [self.tempController presentViewController:controller animated:YES completion:nil];
  82. }
  83. }
  84. //对图片进行裁剪
  85. - (NSData *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize setType:(int)type;
  86. {
  87. UIGraphicsBeginImageContext(newSize);
  88. [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  89. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  90. UIGraphicsEndImageContext();
  91. if(type==1)
  92. return UIImageJPEGRepresentation(newImage, 0.1);
  93. return UIImagePNGRepresentation(newImage);
  94. }
  95. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  96. if ([picker isKindOfClass:[UIImagePickerController class]]) {
  97. [picker dismissViewControllerAnimated:YES completion:nil];
  98. }
  99. }
  100. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  101. NSData* imageData=nil;
  102. UIImage *image = nil;
  103. if (_isEditing) {
  104. image = [info valueForKey:UIImagePickerControllerEditedImage];
  105. }else {
  106. image = [info valueForKey:UIImagePickerControllerOriginalImage];
  107. //解决手机拍照不裁剪旋转90度的问题
  108. image = [image fixOrientation];
  109. }
  110. //判断图片是不是png格式的文件
  111. CGSize size;
  112. size=CGSizeMake(image.size.width/4, image.size.height/4);
  113. if (UIImagePNGRepresentation(image)) {
  114. //返回为png图像。
  115. if (_isOpenHead) {
  116. imageData = [self imageWithImage:image scaledToSize:size setType:0];
  117. }else {
  118. imageData = UIImagePNGRepresentation(image);
  119. }
  120. }else {
  121. //返回为JPEG图像。
  122. if (_isOpenHead) {
  123. imageData=[self imageWithImage:image scaledToSize:size setType:1];
  124. }else {
  125. imageData = UIImageJPEGRepresentation(image, 1.0);
  126. }
  127. }
  128. [picker dismissViewControllerAnimated:YES completion:^{
  129. UIImage* endImage = [UIImage imageWithData:imageData];
  130. if (self.didSelectedCompletionCallback) {
  131. self.didSelectedCompletionCallback(endImage);
  132. }
  133. }];
  134. }
  135. - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
  136. NSString *message = @"";
  137. if (!error) {
  138. message = @"成功保存到相册";
  139. }else
  140. {
  141. message = [error description];
  142. }
  143. NSLog(@"message is %@",message);
  144. }
  145. @end