XWImagePickerSheet.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // XWImagePickerSheet.m
  3. // XWPublishDemo
  4. //
  5. // Created by 邱学伟 on 16/4/15.
  6. // Copyright © 2016年 邱学伟. All rights reserved.
  7. //
  8. #import "XWImagePickerSheet.h"
  9. #import <MobileCoreServices/MobileCoreServices.h>
  10. @interface XWImagePickerSheet ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate,TZImagePickerControllerDelegate>
  11. @end
  12. @implementation XWImagePickerSheet
  13. {
  14. CYImagePickerViewController *imagePickerVc;
  15. }
  16. -(instancetype)init{
  17. self = [super init];
  18. if (self) {
  19. if (!_arrSelected) {
  20. self.arrSelected = [NSMutableArray array];
  21. }
  22. if (!_thumbnailImgArr) {
  23. self.thumbnailImgArr = [NSMutableArray array];
  24. }
  25. }
  26. return self;
  27. }
  28. //显示选择照片提示Sheet
  29. -(void)showImgPickerActionSheetInView:(UIViewController *)controller isPhoto:(BOOL)isPhoto{
  30. NSString *alertTitle = isPhoto ? ASLocalizedString(@"选择照片"): ASLocalizedString(@"选择视频");
  31. NSString *alertCamera = isPhoto ? ASLocalizedString(@"拍照"): ASLocalizedString(@"拍摄视频");
  32. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:alertTitle preferredStyle:UIAlertControllerStyleActionSheet];
  33. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:ASLocalizedString(@"取消")style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  34. }];
  35. UIAlertAction *actionCamera = [UIAlertAction actionWithTitle:alertCamera style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  36. if (!imaPic) {
  37. imaPic = [[UIImagePickerController alloc] init];
  38. }
  39. NSArray *availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];//Camera所支持的Media格式都有哪些,共有两个分别是@"public.image",@"public.movie"
  40. BOOL isShowVC = YES;
  41. if (isPhoto) {
  42. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  43. imaPic.sourceType = UIImagePickerControllerSourceTypeCamera;
  44. imaPic.mediaTypes = [NSArray arrayWithObject:availableMedia[0]];//设置媒体类型为public.movie
  45. imaPic.delegate = self;
  46. }
  47. if (self.arrSelected.count > 0)
  48. {
  49. PHAsset *set = self.arrSelected.firstObject;
  50. if (set.mediaType == PHAssetMediaTypeVideo)
  51. {
  52. // [MBProgressHUD showTopTipMessage:ASLocalizedString(@"选择视频不能再选择图片")];
  53. [[BGHUDHelper sharedInstance]tipMessage:ASLocalizedString(@"选择视频不能再选择图片")];
  54. isShowVC = NO;
  55. }
  56. }
  57. }else{
  58. if (self.arrSelected.count > 0)
  59. {
  60. PHAsset *set = self.arrSelected.firstObject;
  61. if (set.mediaType == PHAssetMediaTypeImage)
  62. {
  63. // [MBProgressHUD showTopTipMessage:ASLocalizedString(@"选择图片不能再选择视频")];
  64. [[BGHUDHelper sharedInstance]tipMessage:ASLocalizedString(@"选择图片不能再选择视频")];
  65. isShowVC = NO;
  66. }
  67. }
  68. imaPic.sourceType = UIImagePickerControllerSourceTypeCamera;//sourcetype有三种分别是camera,photoLibrary和photoAlbum
  69. // ipc.cameraDevice = UIImagePickerControllerCameraDeviceFront;
  70. imaPic.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];//设置媒体类型为public.movie
  71. imaPic.videoMaximumDuration = 600.0f;//10分钟
  72. imaPic.delegate = self;//设置委托
  73. }
  74. if (isShowVC) [viewController presentViewController:imaPic animated:NO completion:nil];
  75. }];
  76. UIAlertAction *actionAlbum = [UIAlertAction actionWithTitle:[NSString stringWithFormat:ASLocalizedString(@"相册")] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  77. // [self loadImgDataAndShowAllGroup];
  78. BOOL isShowVC = YES;
  79. if (isPhoto) {
  80. if (self.arrSelected.count > 0)
  81. {
  82. PHAsset *set = self.arrSelected.firstObject;
  83. if (set.mediaType == PHAssetMediaTypeVideo)
  84. {
  85. // [MBProgressHUD showTopTipMessage:ASLocalizedString(@"选择视频不能再选择图片")];
  86. [[BGHUDHelper sharedInstance]tipMessage:ASLocalizedString(@"选择视频不能再选择图片")];
  87. isShowVC = NO;
  88. }
  89. }
  90. }else{
  91. if (self.arrSelected.count > 0)
  92. {
  93. PHAsset *set = self.arrSelected.firstObject;
  94. if (set.mediaType == PHAssetMediaTypeImage)
  95. {
  96. // [MBProgressHUD showTopTipMessage:ASLocalizedString(@"选择图片不能再选择视频")];
  97. [[BGHUDHelper sharedInstance]tipMessage:ASLocalizedString(@"选择图片不能再选择视频")];
  98. isShowVC = NO;
  99. }
  100. }
  101. }
  102. [self newShowPhotosWithStatus:isPhoto isShowVC:isShowVC];
  103. }];
  104. [alertController addAction:actionCancel];
  105. [alertController addAction:actionCamera];
  106. [alertController addAction:actionAlbum];
  107. viewController = controller;
  108. [viewController presentViewController:alertController animated:YES completion:nil];
  109. }
  110. - (void)newShowPhotosWithStatus:(BOOL)status isShowVC:(BOOL)isShowVC
  111. {
  112. imagePickerVc = [[CYImagePickerViewController alloc] initWithMaxImagesCount:_maxCount delegate:self];
  113. [imagePickerVc.navigationController.navigationBar setTintColor:[UIColor blackColor]];
  114. imagePickerVc.naviBgColor = [UIColor blackColor];
  115. imagePickerVc.allowPickingImage = status;
  116. imagePickerVc.allowPickingVideo = !status;
  117. // You can get the photos by block, the same as by delegate.
  118. // 你可以通过block或者代理,来得到用户选择的照片.
  119. __block typeof(self)blockself =self;
  120. [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
  121. BOOL ishaveVideo =NO,ishaveImage =NO;
  122. NSMutableArray *c_assets = [assets mutableCopy];
  123. NSMutableArray *c_phots = [photos mutableCopy];
  124. for (id c_set in assets)
  125. {
  126. if ([c_set isKindOfClass:[PHAsset class]])
  127. {
  128. PHAsset *set =(PHAsset *)c_set;
  129. if (set.mediaType == PHAssetMediaTypeVideo)
  130. {
  131. ishaveVideo =YES;
  132. //需要移除视频
  133. NSInteger index =[assets indexOfObject:c_set];
  134. [c_phots removeObjectAtIndex:index];
  135. [c_assets removeObject:c_set];
  136. }
  137. if (set.mediaType == PHAssetMediaTypeImage)
  138. {
  139. ishaveImage = YES;
  140. }
  141. }
  142. }
  143. blockself.arrSelected =[c_assets mutableCopy];
  144. blockself.thumbnailImgArr = [c_phots mutableCopy];
  145. if (blockself.delegate && [blockself.delegate respondsToSelector:@selector(getSelectImageWithALAssetArray:thumbnailImageArray:)]) {
  146. [blockself.delegate getSelectImageWithALAssetArray:blockself.arrSelected thumbnailImageArray:blockself.thumbnailImgArr];
  147. }
  148. }];
  149. //视频回调
  150. [imagePickerVc setDidFinishPickingVideoHandle:^(UIImage *coverImage, PHAsset *asset) {
  151. blockself.arrSelected =[@[asset] mutableCopy];
  152. if (blockself.delegate && [blockself.delegate respondsToSelector:@selector(getSelectVideoWith:thumbImage:)]) {
  153. [blockself.delegate getSelectVideoWith:asset thumbImage:@[coverImage]];
  154. }
  155. }];
  156. imagePickerVc.naviTitleColor = [UIColor blackColor];
  157. imagePickerVc.barItemTextColor = [UIColor blackColor];
  158. imagePickerVc.oKButtonTitleColorNormal = kAppNewMainColor;
  159. // [UIColor blackColor];
  160. imagePickerVc.showSelectBtn = status;
  161. if (_arrSelected)
  162. {
  163. // NSLog(@"%@",_arrSelected);
  164. // ALAsset *asset = _arrSelected.firstObject;
  165. // NSLog(@"%@",asset);
  166. imagePickerVc.selectedAssets =_arrSelected;
  167. }
  168. if (isShowVC) [viewController presentViewController:imagePickerVc animated:YES completion:nil];
  169. }
  170. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  171. {
  172. [imaPic dismissViewControllerAnimated:NO completion:nil];
  173. }
  174. - (void)newFinishedChooseImage:(NSArray *)assets
  175. {
  176. //正方形缩略图
  177. NSMutableArray *thumbnailImgArr = [NSMutableArray array];
  178. for (ALAsset *set in _arrSelected) {
  179. CGImageRef cgImg = [set thumbnail];
  180. UIImage* image = [UIImage imageWithCGImage: cgImg];
  181. [thumbnailImgArr addObject:image];
  182. }
  183. if (self.delegate && [self.delegate respondsToSelector:@selector(getSelectImageWithALAssetArray:thumbnailImageArray:)]) {
  184. [self.delegate getSelectImageWithALAssetArray:_arrSelected thumbnailImageArray:thumbnailImgArr];
  185. }
  186. }
  187. #pragma mark - 加载照片数据
  188. - (void)loadImgDataAndShowAllGroup{
  189. if (!_arrSelected) {
  190. self.arrSelected = [NSMutableArray array];
  191. }
  192. [[MImaLibTool shareMImaLibTool] getAllGroupWithArrObj:^(NSArray *arrObj) {
  193. if (arrObj && arrObj.count > 0) {
  194. self.arrGroup = arrObj;
  195. if ( self.arrGroup.count > 0) {
  196. MShowAllGroup *svc = [[MShowAllGroup alloc] initWithArrGroup:self.arrGroup arrSelected:self.arrSelected];
  197. svc.delegate = self;
  198. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:svc];
  199. if (_arrSelected) {
  200. svc.arrSeleted = _arrSelected;
  201. svc.mvc.arrSelected = _arrSelected;
  202. }
  203. svc.maxCout = _maxCount;
  204. [viewController presentViewController:nav animated:YES completion:nil];
  205. }
  206. }
  207. }];
  208. }
  209. #pragma mark - 拍照获得数据
  210. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
  211. //获取用户选择或拍摄的是照片还是视频
  212. NSString *mediaType = info[UIImagePickerControllerMediaType];
  213. if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
  214. UIImage *theImage = nil;
  215. // 判断,图片是否允许修改
  216. if ([picker allowsEditing]){
  217. //获取用户编辑之后的图像
  218. theImage = [info objectForKey:UIImagePickerControllerEditedImage];
  219. } else {
  220. // 照片的元数据参数
  221. theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
  222. }
  223. if (theImage) {
  224. //保存图片到相册中
  225. MImaLibTool *imgLibTool = [MImaLibTool shareMImaLibTool];
  226. [imgLibTool.lib writeImageToSavedPhotosAlbum:[theImage CGImage] orientation:(ALAssetOrientation)[theImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
  227. if (error) {
  228. } else {
  229. //获取图片路径
  230. PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil];
  231. if (result) {
  232. PHAsset *asset = [result lastObject];
  233. if (asset) {
  234. [_arrSelected addObject:asset];
  235. // [_thumbnailImgArr removeAllObjects];
  236. // [self finishSelectImg];
  237. [self fetchImageWithAsset:asset imageBlock:^{
  238. }];
  239. [picker dismissViewControllerAnimated:NO completion:nil];
  240. }
  241. }
  242. }
  243. }];
  244. }
  245. }else{
  246. NSURL * url = [info objectForKey:UIImagePickerControllerMediaURL];
  247. __block typeof(self)blockself =self;
  248. //保存图片到相册中
  249. MImaLibTool *imgLibTool = [MImaLibTool shareMImaLibTool];
  250. [imgLibTool.lib writeVideoAtPathToSavedPhotosAlbum:info[UIImagePickerControllerMediaURL] completionBlock:^(NSURL *assetURL, NSError *error) {
  251. if (error) {
  252. } else {
  253. //获取视频路径
  254. PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil];
  255. if (result) {
  256. PHAsset *asset = [result lastObject];
  257. if (asset) {
  258. [_arrSelected removeAllObjects];
  259. [_thumbnailImgArr removeAllObjects];
  260. [_arrSelected addObject:asset];
  261. [self fetchImageWithAsset:asset imageBlock:^{
  262. }];
  263. [picker dismissViewControllerAnimated:NO completion:nil];
  264. }
  265. }
  266. // //获取视频路径
  267. // [imgLibTool.lib assetForURL:assetURL resultBlock:^(ALAsset *asset) {
  268. // if (asset) {
  269. // [_arrSelected addObject:asset];
  270. // [self finishSelectImg];
  271. //
  272. // [imaPic dismissViewControllerAnimated:NO completion:nil];
  273. // }else{
  274. //
  275. // }
  276. // } failureBlock:^(NSError *error) {
  277. //
  278. // }];
  279. }
  280. }];;
  281. }
  282. }
  283. - (void)fetchImageWithAsset:(PHAsset*)mAsset imageBlock:(void(^)())imageBlock {
  284. [[PHImageManager defaultManager] requestImageDataForAsset:mAsset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  285. UIImage* image = [UIImage imageWithData:imageData];
  286. if (orientation != UIImageOrientationUp) {
  287. // 尽然弯了,那就板正一下
  288. image = [image fixOrientation];
  289. // // 新的 数据信息 (不准确的)
  290. // imageData = UIImageJPEGRepresentation(image, 0.5);
  291. }
  292. [_thumbnailImgArr addObject:image];
  293. if (self.delegate && [self.delegate respondsToSelector:@selector(getSelectImageWithALAssetArray:thumbnailImageArray:)]) {
  294. [self.delegate getSelectImageWithALAssetArray:_arrSelected thumbnailImageArray:_thumbnailImgArr];
  295. }
  296. // 直接得到最终的 NSData 数据
  297. if (imageBlock) {
  298. imageBlock();
  299. }
  300. }];
  301. }
  302. @end