XWPublishBaseController.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. //
  2. // XWPublishBaseController.m
  3. // XWPublishDemo
  4. //
  5. // Created by 邱学伟 on 16/4/15.
  6. // Copyright © 2016年 邱学伟. All rights reserved.
  7. //
  8. #import "XWPublishBaseController.h"
  9. #import "JZVideoPlayerView.h"
  10. #define column 3
  11. #define itemWidth ([UIScreen mainScreen].bounds.size.width-10*4) /3
  12. @interface XWPublishBaseController ()<UICollectionViewDelegate,UICollectionViewDataSource,JJPhotoDelegate,XWImagePickerSheetDelegate,JZPlayerViewDelegate>{
  13. JZVideoPlayerView *_jzPlayer;
  14. // UIView *loadView;
  15. UIActivityIndicatorView *activityIndicatorView;
  16. NSString *pushImageName;
  17. //添加图片提示
  18. UILabel *addImageStrLabel;
  19. }
  20. @property (nonatomic, strong) XWImagePickerSheet *imgPickerActionSheet;
  21. @property (nonatomic, strong) UIView *playBGView;
  22. @property(nonatomic, assign) BOOL isPlaying;
  23. @property(nonatomic, assign) BOOL playerIsBuffering;
  24. @property(nonatomic, strong) UIButton *playBtn;
  25. @end
  26. @implementation XWPublishBaseController{
  27. /**播放器*/
  28. AVPlayer *_player;
  29. /**playerLayer*/
  30. AVPlayerLayer *_playerLayer;
  31. /**播放器item*/
  32. AVPlayerItem *_playerItem;
  33. }
  34. static NSString * const reuseIdentifier = @"XWPhotoCell";
  35. -(instancetype)init{
  36. self = [super init];
  37. if (self) {
  38. if (!_showActionSheetViewController) {
  39. _showActionSheetViewController = self;
  40. }
  41. }
  42. return self;
  43. }
  44. - (void)viewDidLoad {
  45. [super viewDidLoad];
  46. // Do any additional setup after loading the view.
  47. }
  48. //初始化collectionView
  49. -(void)initPickerView{
  50. _showActionSheetViewController = self;
  51. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  52. self.pickerCollectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
  53. if (_showInView) {
  54. [_showInView addSubview:self.pickerCollectionView];
  55. }else{
  56. [self.view addSubview:self.pickerCollectionView];
  57. }
  58. self.pickerCollectionView.delegate=self;
  59. self.pickerCollectionView.dataSource=self;
  60. self.pickerCollectionView.backgroundColor = [UIColor whiteColor];
  61. if(_imageArray.count == 0)
  62. {
  63. _imageArray = [NSMutableArray array];
  64. }
  65. if(_bigImageArray.count == 0)
  66. {
  67. _bigImageArray = [NSMutableArray array];
  68. }
  69. pushImageName = @"plus";
  70. _pickerCollectionView.scrollEnabled = NO;
  71. //添加图片提示
  72. addImageStrLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 50, 120, 20)];
  73. addImageStrLabel.text = ASLocalizedString(@"添加图片/视频");
  74. addImageStrLabel.textColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0];
  75. // [self.pickerCollectionView addSubview:addImageStrLabel];
  76. }
  77. - (void)didReceiveMemoryWarning {
  78. [super didReceiveMemoryWarning];
  79. // Dispose of any resources that can be recreated.
  80. }
  81. #pragma mark <UICollectionViewDataSource>
  82. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  83. return 1;
  84. }
  85. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  86. if (_imageArray.count >= self.maxCount) {
  87. return _imageArray.count;
  88. }
  89. return _imageArray.count+1;
  90. }
  91. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  92. // Register nib file for the cell
  93. UINib *nib = [UINib nibWithNibName:@"XWPhotoCell" bundle: [NSBundle mainBundle]];
  94. [collectionView registerNib:nib forCellWithReuseIdentifier:@"XWPhotoCell"];
  95. // Set up the reuse identifier
  96. XWPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"XWPhotoCell" forIndexPath:indexPath];
  97. if (indexPath.row == _imageArray.count) {
  98. // [cell.profilePhoto setImage:[UIImage imageNamed:pushImageName]];
  99. cell.profilePhoto.image = [UIImage imageNamed:@"mine_edit_addPhoto"];
  100. // IMAGE_NAMED(@"mine_edit_addPhoto");
  101. cell.closeButton.hidden = YES;
  102. //没有任何图片
  103. if (_imageArray.count == 0) {
  104. addImageStrLabel.hidden = NO;
  105. }
  106. else{
  107. addImageStrLabel.hidden = YES;
  108. }
  109. }
  110. else{
  111. [cell.profilePhoto setImage:_imageArray[indexPath.item]];
  112. cell.closeButton.hidden = NO;
  113. }
  114. [cell setBigImgViewWithImage:nil];
  115. cell.profilePhoto.tag = [indexPath item];
  116. //添加图片cell点击事件
  117. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapProfileImage:)];
  118. singleTap.numberOfTapsRequired = 1;
  119. cell.profilePhoto .userInteractionEnabled = YES;
  120. [cell.profilePhoto addGestureRecognizer:singleTap];
  121. cell.closeButton.tag = [indexPath item];
  122. [cell.closeButton addTarget:self action:@selector(deletePhoto:) forControlEvents:UIControlEventTouchUpInside];
  123. return cell;
  124. }
  125. #pragma mark <UICollectionViewDelegate>
  126. //定义每个UICollectionView 的大小
  127. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  128. {
  129. return CGSizeMake(itemWidth ,itemWidth);
  130. }
  131. //定义每个UICollectionView 的 margin
  132. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  133. {
  134. return UIEdgeInsetsMake(10, 10, 10, 10);
  135. }
  136. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  137. return 10;
  138. }
  139. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  140. return 0;
  141. }
  142. #pragma mark - 图片cell点击事件
  143. //点击图片看大图
  144. - (void) tapProfileImage:(UITapGestureRecognizer *)gestureRecognizer{
  145. [self.view endEditing:YES];
  146. UIImageView *tableGridImage = (UIImageView*)gestureRecognizer.view;
  147. NSInteger index = tableGridImage.tag;
  148. if (index == (_imageArray.count)) {
  149. [self.view endEditing:YES];
  150. //添加新图片
  151. [self addNewImg];
  152. }
  153. else{
  154. //点击放大查看
  155. XWPhotoCell *cell = (XWPhotoCell*)[_pickerCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
  156. if (!cell.BigImgView || !cell.BigImgView.image) {
  157. [self getBigIamgeWithPhasset:_arrSelected[index] isSubmit:NO callbackImg:^(id img, bool isImg, id videoStr) {
  158. if (isImg) {
  159. [cell setBigImgViewWithImage:[UIImage imageWithData:img]];
  160. JJPhotoManeger *mg = [JJPhotoManeger maneger];
  161. mg.delegate = self;
  162. [mg showLocalPhotoViewer:@[cell.BigImgView] selecImageindex:0];
  163. }else{
  164. // if (videoStr) {
  165. // NSURL *url = videoStr;
  166. // [self setPlayerWithUrl:url];
  167. // }
  168. }
  169. }];
  170. }else
  171. {
  172. JJPhotoManeger *mg = [JJPhotoManeger maneger];
  173. mg.delegate = self;
  174. [mg showLocalPhotoViewer:@[cell.BigImgView] selecImageindex:0];
  175. }
  176. }
  177. }
  178. -(void)showBigVideo{
  179. if (!_playBGView) {
  180. _playBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  181. _playBGView.backgroundColor = kBlackColor;
  182. UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 35, kRealValue(40), kRealValue(40))];
  183. [backBtn setImage:[UIImage imageNamed:@"me_society_back"] forState:UIControlStateNormal];
  184. [backBtn addTarget:self action:@selector(clickHiddeVideo:) forControlEvents:UIControlEventTouchUpInside];
  185. [_playBGView addSubview:backBtn];
  186. [_playBGView addSubview:self.playBtn];
  187. [self.view addSubview:_playBGView];
  188. }
  189. activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
  190. [activityIndicatorView setCenter:_playBGView.center];
  191. [activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
  192. [activityIndicatorView startAnimating];
  193. [_playBGView addSubview:activityIndicatorView];
  194. }
  195. -(void)clickHiddeVideo:(UIButton *)sender{
  196. [self removePlayerView];
  197. }
  198. -(void)tapImg:(UIGestureRecognizer *)sender{
  199. // [self.view endEditing:YES];
  200. // if (!_playBGView) {
  201. // [self setPlayerWithUrl:_url];
  202. // }
  203. [_player play];
  204. self.playBtn.hidden = YES;
  205. }
  206. -(void)setPlayerWithUrl:(AVURLAsset *)asset{
  207. [_player pause];
  208. _isPlaying = YES;
  209. if (!_player) {
  210. _playerItem = [AVPlayerItem playerItemWithAsset:asset];
  211. // playerItemWithURL:url];
  212. _player = [AVPlayer playerWithPlayerItem:_playerItem];
  213. _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
  214. _playerLayer.frame = CGRectMake(0, 0, kScreenW,kScreenH);
  215. _playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
  216. [self addObserverToPlayerItem:_playerItem];
  217. [_playBGView.layer addSublayer:_playerLayer];
  218. }
  219. //拖动
  220. // UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGuestre:)];
  221. // [_playBGView addGestureRecognizer:pan];
  222. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImg:)];
  223. [_playBGView addGestureRecognizer:tap];
  224. }
  225. -(void)playbackFinished:(NSNotification *)notification{
  226. NSLog(ASLocalizedString(@"视频播放完成"));
  227. [_player play];
  228. }
  229. -(void)removePlayerView{
  230. if (_playBGView) {
  231. [_player pause];
  232. [_playBGView removeAllSubViews];
  233. [_playBGView.layer removeAllSublayers];
  234. [_playBGView removeFromSuperview];
  235. _playBGView = nil;
  236. _player = nil;
  237. _playerLayer = nil;
  238. [self removeObserverToPlayerItem:_playerItem];
  239. _playerItem = nil;
  240. }
  241. }
  242. - (UIImage*)getBigIamgeWithALAsset:(ALAsset*)set{
  243. //压缩
  244. // 需传入方向和缩放比例,否则方向和尺寸都不对
  245. UIImage *img = [UIImage imageWithCGImage:set.defaultRepresentation.fullResolutionImage
  246. scale:set.defaultRepresentation.scale
  247. orientation:(UIImageOrientation)set.defaultRepresentation.orientation];
  248. NSData *imageData = UIImageJPEGRepresentation(img, 0.5);
  249. [_bigImgDataArray addObject:imageData];
  250. return [UIImage imageWithData:imageData];
  251. }
  252. - (void)getBigIamgeWithPhasset:(PHAsset*)set isSubmit:(bool)isSubmit callbackImg:(void(^)(id img,bool isImg,id videoStr))imageBlock
  253. {
  254. if (set.mediaType == PHAssetMediaTypeVideo)
  255. {
  256. if (!isSubmit) [self showBigVideo];
  257. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  258. options.version = PHImageRequestOptionsVersionCurrent;
  259. options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
  260. options.networkAccessAllowed = YES;
  261. PHImageManager *manager = [PHImageManager defaultManager];
  262. [manager requestAVAssetForVideo:set options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
  263. AVURLAsset *urlAsset = (AVURLAsset *)asset;
  264. NSURL *url = urlAsset.URL;
  265. if (!isSubmit) [self setPlayerWithUrl:urlAsset];
  266. NSLog(@"%@",url);
  267. imageBlock(nil,NO,url);
  268. }];
  269. }else
  270. {
  271. [[PHImageManager defaultManager]requestImageDataForAsset:set options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  272. UIImage* image = [UIImage imageWithData:imageData];
  273. image =[XWPublishBaseController fixOrientation:image];
  274. imageBlock(UIImageJPEGRepresentation(image, 0.5),YES,nil);
  275. }];
  276. }
  277. }
  278. //-(void)initJZPlayerWithUrl:(NSURL *)url{
  279. // [_jzPlayer resetContentViewWithContentURL:url];
  280. //
  281. // [_jzPlayer play];
  282. //}
  283. /**
  284. * 给AVPlayerItem添加监控
  285. *
  286. * @param playerItem AVPlayerItem对象
  287. */
  288. -(void)addObserverToPlayerItem:(AVPlayerItem *)playerItem{
  289. //监控状态属性,注意AVPlayer也有一个status属性,通过监控它的status也可以获得播放状态
  290. [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
  291. //监控网络加载情况属性
  292. [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
  293. //监控缓冲区大小
  294. [playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];//监控缓冲区大小
  295. [playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
  296. // [self performSelectorInBackground:@selector(initPlayTime) withObject:nil];
  297. }
  298. #pragma mark - 观察视频播放各个监听触发
  299. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
  300. if ([keyPath isEqualToString:@"status"]) {//播放状态
  301. AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
  302. switch (status) {
  303. case AVPlayerStatusFailed:
  304. NSLog(ASLocalizedString(@"播放失败"));
  305. // [loadView setHidden:NO];
  306. activityIndicatorView.hidden = YES;
  307. break;
  308. case AVPlayerStatusReadyToPlay:
  309. NSLog(ASLocalizedString(@"正在播放...视频中长度为:%f"),CMTimeGetSeconds(_playerItem.duration));
  310. // [loadView setHidden:YES];
  311. // [_player play];
  312. activityIndicatorView.hidden = YES;
  313. break;
  314. default:
  315. NSLog(@"default:");
  316. break;
  317. }
  318. }else if ([keyPath isEqualToString:@"loadedTimeRanges"]){//缓冲
  319. NSArray *array = _playerItem.loadedTimeRanges;
  320. CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];//本次缓冲时间
  321. float startSeconds = CMTimeGetSeconds(timeRange.start);
  322. float durationSeconds = CMTimeGetSeconds(timeRange.duration);
  323. NSTimeInterval totalBuffer = startSeconds + durationSeconds;//缓冲总长度
  324. float durationTime = CMTimeGetSeconds([[_player currentItem] duration]);//总时间
  325. NSLog(ASLocalizedString(@"共缓冲:%.2f,总时长为:%f"),totalBuffer,durationTime);
  326. // [self.loadProgressView setProgress:totalBuffer/durationTime animated:YES];
  327. // if (self.playerIsBuffering && self.isPlaying) {
  328. // [_player play];
  329. // self.playerIsBuffering = NO;
  330. // }
  331. }else if ([keyPath isEqualToString:@"playbackBufferEmpty"]){
  332. if(_player.currentItem.playbackBufferEmpty){
  333. NSLog(ASLocalizedString(@"缓冲区为空"));
  334. self.playerIsBuffering = YES;
  335. }else{
  336. NSLog(ASLocalizedString(@"缓冲区不为空======"));
  337. }
  338. }else if ([keyPath isEqualToString:@"playbackLikelyToKeepUp"]) {
  339. if (!_playerItem.playbackLikelyToKeepUp) {
  340. _playerIsBuffering = YES;
  341. // [_player play];
  342. }else {
  343. _playerIsBuffering = NO;
  344. }
  345. }else{
  346. NSLog(@"++++++++++");
  347. }
  348. }
  349. -(NSString*)getStringFromCMTime:(CMTime)time
  350. {
  351. Float64 currentSeconds = CMTimeGetSeconds(time);
  352. int mins = currentSeconds/60.0;
  353. int hours = mins / 60.0f;
  354. int secs = fmodf(currentSeconds, 60.0);
  355. mins = fmodf(mins, 60.0f);
  356. NSString *hoursString = hours < 10 ? [NSString stringWithFormat:@"0%d", hours] : [NSString stringWithFormat:@"%d", hours];
  357. NSString *minsString = mins < 10 ? [NSString stringWithFormat:@"0%d", mins] : [NSString stringWithFormat:@"%d", mins];
  358. NSString *secsString = secs < 10 ? [NSString stringWithFormat:@"0%d", secs] : [NSString stringWithFormat:@"%d", secs];
  359. return [NSString stringWithFormat:@"%@:%@:%@", hoursString,minsString, secsString];
  360. }
  361. //处理图片旋转的问题
  362. + (UIImage *)fixOrientation:(UIImage *)aImage {
  363. // No-op if the orientation is already correct
  364. if (aImage.imageOrientation == UIImageOrientationUp)
  365. return aImage;
  366. // We need to calculate the proper transformation to make the image upright.
  367. // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
  368. CGAffineTransform transform = CGAffineTransformIdentity;
  369. switch (aImage.imageOrientation) {
  370. case UIImageOrientationDown:
  371. case UIImageOrientationDownMirrored:
  372. transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
  373. transform = CGAffineTransformRotate(transform, M_PI);
  374. break;
  375. case UIImageOrientationLeft:
  376. case UIImageOrientationLeftMirrored:
  377. transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
  378. transform = CGAffineTransformRotate(transform, M_PI_2);
  379. break;
  380. case UIImageOrientationRight:
  381. case UIImageOrientationRightMirrored:
  382. transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
  383. transform = CGAffineTransformRotate(transform, -M_PI_2);
  384. break;
  385. default:
  386. break;
  387. }
  388. switch (aImage.imageOrientation) {
  389. case UIImageOrientationUpMirrored:
  390. case UIImageOrientationDownMirrored:
  391. transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
  392. transform = CGAffineTransformScale(transform, -1, 1);
  393. break;
  394. case UIImageOrientationLeftMirrored:
  395. case UIImageOrientationRightMirrored:
  396. transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
  397. transform = CGAffineTransformScale(transform, -1, 1);
  398. break;
  399. default:
  400. break;
  401. }
  402. // Now we draw the underlying CGImage into a new context, applying the transform
  403. // calculated above.
  404. CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
  405. CGImageGetBitsPerComponent(aImage.CGImage), 0,
  406. CGImageGetColorSpace(aImage.CGImage),
  407. CGImageGetBitmapInfo(aImage.CGImage));
  408. CGContextConcatCTM(ctx, transform);
  409. switch (aImage.imageOrientation) {
  410. case UIImageOrientationLeft:
  411. case UIImageOrientationLeftMirrored:
  412. case UIImageOrientationRight:
  413. case UIImageOrientationRightMirrored:
  414. // Grr...
  415. CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
  416. break;
  417. default:
  418. CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
  419. break;
  420. }
  421. // And now we just create a new UIImage from the drawing context
  422. CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
  423. UIImage *img = [UIImage imageWithCGImage:cgimg];
  424. CGContextRelease(ctx);
  425. CGImageRelease(cgimg);
  426. return img;
  427. }
  428. #pragma mark - 选择图片
  429. - (void)addNewImg{
  430. if (!_imgPickerActionSheet) {
  431. _imgPickerActionSheet = [[XWImagePickerSheet alloc] init];
  432. _imgPickerActionSheet.delegate = self;
  433. }
  434. if (_arrSelected) {
  435. _imgPickerActionSheet.arrSelected = _arrSelected;
  436. }
  437. _imgPickerActionSheet.maxCount = _maxCount;
  438. [_imgPickerActionSheet showImgPickerActionSheetInView:_showActionSheetViewController isPhoto:YES];
  439. }
  440. -(void)addNewVideo{
  441. if (!_imgPickerActionSheet) {
  442. _imgPickerActionSheet = [[XWImagePickerSheet alloc] init];
  443. _imgPickerActionSheet.delegate = self;
  444. }
  445. if (_arrSelected) {
  446. _imgPickerActionSheet.arrSelected = _arrSelected;
  447. }
  448. // HXPhotoManagerSelectedTypeVideo
  449. _imgPickerActionSheet.maxCount = _maxCount;
  450. [_imgPickerActionSheet showImgPickerActionSheetInView:_showActionSheetViewController isPhoto:NO];
  451. }
  452. #pragma mark - 删除照片
  453. - (void)deletePhoto:(UIButton *)sender{
  454. if ([self collectionView:self.pickerCollectionView numberOfItemsInSection:0] <= _arrSelected.count) {
  455. [_imageArray removeObjectAtIndex:sender.tag];
  456. [_arrSelected removeObjectAtIndex:sender.tag];
  457. [self.pickerCollectionView reloadData];
  458. [self changeCollectionViewHeight];
  459. return;
  460. }
  461. [_imageArray removeObjectAtIndex:sender.tag];
  462. [_arrSelected removeObjectAtIndex:sender.tag];
  463. [_pickerCollectionView performBatchUpdates:^{
  464. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0];
  465. [self->_pickerCollectionView deleteItemsAtIndexPaths:@[indexPath]];
  466. } completion:^(BOOL finished) {
  467. [self->_pickerCollectionView reloadData];
  468. [self changeCollectionViewHeight];
  469. }];
  470. }
  471. #pragma mark - 改变view,collectionView高度
  472. - (void)changeCollectionViewHeight{
  473. int a =(int)(_arrSelected.count)/column+1;
  474. if (_arrSelected.count == self.maxCount + 1) {
  475. a =(int)(_arrSelected.count)/column;
  476. }
  477. if (_collectionFrameY) {
  478. _pickerCollectionView.frame = CGRectMake(0, _collectionFrameY, [UIScreen mainScreen].bounds.size.width, a* (10 + itemWidth)+10);
  479. } else {
  480. _pickerCollectionView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, a* (10 + itemWidth)+10);
  481. }
  482. [self pickerViewFrameChanged];
  483. }
  484. /**
  485. * 相册完成选择得到图片
  486. */
  487. -(void)getSelectImageWithALAssetArray:(NSArray *)ALAssetArray thumbnailImageArray:(NSArray *)thumbnailImgArray{
  488. //(ALAsset)类型 Array
  489. _arrSelected = [NSMutableArray arrayWithArray:ALAssetArray];
  490. //正方形缩略图 Array
  491. _imageArray = [NSMutableArray arrayWithArray:thumbnailImgArray] ;
  492. [self.pickerCollectionView reloadData];
  493. [self changeCollectionViewHeight];
  494. }
  495. - (void)getSelectVideoWith:(PHAsset *)asset thumbImage:(NSArray *)thumbimages
  496. {
  497. _arrSelected = [@[asset] mutableCopy];
  498. _imageArray = [NSMutableArray arrayWithArray:thumbimages] ;
  499. [self.pickerCollectionView reloadData];
  500. [self changeCollectionViewHeight];
  501. }
  502. - (void)pickerViewFrameChanged{
  503. }
  504. - (void)updatePickerViewFrameY:(CGFloat)Y{
  505. _collectionFrameY = Y;
  506. if (_arrSelected.count == 0) {
  507. _pickerCollectionView.frame = CGRectMake(0, Y, [UIScreen mainScreen].bounds.size.width, 0);
  508. }else{
  509. int a =(int)(_arrSelected.count)/column+1;
  510. if (_arrSelected.count == self.maxCount) {
  511. a =(int)(_arrSelected.count)/column;
  512. }
  513. _pickerCollectionView.frame = CGRectMake(0, Y, [UIScreen mainScreen].bounds.size.width, a* (10 + itemWidth)+10);
  514. }
  515. }
  516. #pragma mark - 防止奔溃处理
  517. -(void)photoViwerWilldealloc:(NSInteger)selecedImageViewIndex
  518. {
  519. NSLog(ASLocalizedString(@"最后一张观看的图片的index是:%zd"),selecedImageViewIndex);
  520. }
  521. - (UIImage *)compressImage:(UIImage *)image toMaxFileSize:(NSInteger)maxFileSize {
  522. CGFloat compression = 0.9f;
  523. CGFloat maxCompression = 0.1f;
  524. NSData *imageData = UIImageJPEGRepresentation(image, compression);
  525. while ([imageData length] > maxFileSize && compression > maxCompression) {
  526. compression -= 0.1;
  527. imageData = UIImageJPEGRepresentation(image, compression);
  528. }
  529. UIImage *compressedImage = [UIImage imageWithData:imageData];
  530. return compressedImage;
  531. }
  532. //获得大图
  533. - (NSArray*)getBigImageArrayWithALAssetArray:(NSArray*)ALAssetArray{
  534. _bigImgDataArray = [NSMutableArray array];
  535. NSMutableArray *bigImgArr = [NSMutableArray array];
  536. for (id set in ALAssetArray) {
  537. if ([set isKindOfClass:[ALAsset class]])
  538. {
  539. [bigImgArr addObject:[self getBigIamgeWithALAsset:set]];
  540. }
  541. }
  542. _bigImageArray = bigImgArr;
  543. return _bigImgDataArray;
  544. }
  545. -(void)PhassetgetBigImageArray:(NSArray *)PHssets isSubmit:(bool)isSubmit callBack:(void (^)(NSArray *, bool))PHassetBlock
  546. {
  547. __block typeof(self)blockself =self;
  548. _bigImageArray =[NSMutableArray array];
  549. int i =0;
  550. for (id set in PHssets)
  551. {
  552. if ([set isKindOfClass:[PHAsset class]])
  553. {
  554. i++;
  555. [self getBigIamgeWithPhasset:set isSubmit:isSubmit callbackImg:^(id img, bool isImg, id videoStr) {
  556. // if (isImg) {
  557. if (img) {
  558. [blockself.bigImageArray addObject:img];
  559. }else{
  560. NSURL *url = videoStr;
  561. if (url) {
  562. [blockself.bigImageArray addObject:[NSData dataWithContentsOfURL:url]];
  563. }
  564. }
  565. NSLog(@"%@",blockself.bigImageArray);
  566. if (blockself.bigImageArray.count >= i)
  567. {
  568. PHassetBlock(blockself.bigImageArray,isImg);
  569. }
  570. // }
  571. }];
  572. }
  573. }
  574. }
  575. -(UIButton *)playBtn{
  576. if (!_playBtn) {
  577. _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  578. [_playBtn setImage:[UIImage imageNamed:@"aio_record_play_nor"] forState:UIControlStateNormal];
  579. _playBtn.frame = CGRectMake(0, 0, kRealValue(50), kRealValue(50));
  580. _playBtn.center = CGPointMake(kScreenW / 2, kScreenH / 2);
  581. [_playBtn addTarget:self action:@selector(tapImg:) forControlEvents:UIControlEventTouchUpInside];
  582. }
  583. return _playBtn;
  584. }
  585. #pragma mark - 获得选中图片各个尺寸
  586. - (NSArray*)getALAssetArray{
  587. return _arrSelected;
  588. }
  589. - (NSArray*)getBigImageArray{
  590. if (_bigImageArray.count>0) {
  591. return _bigImageArray;
  592. }
  593. return [self getBigImageArrayWithALAssetArray:_arrSelected];
  594. }
  595. - (NSArray*)getSmallImageArray{
  596. return _imageArray;
  597. }
  598. - (CGRect)getPickerViewFrame{
  599. return self.pickerCollectionView.frame;
  600. }
  601. - (void)dealloc
  602. {
  603. _imgPickerActionSheet.delegate =nil;
  604. _imgPickerActionSheet =nil;
  605. _showActionSheetViewController =nil;
  606. [self removeObserverToPlayerItem:_playerItem];
  607. // [_player removeTimeObserver:playbackObserver];
  608. [[NSNotificationCenter defaultCenter] removeObserver:self];
  609. }
  610. /**
  611. * 移除KVO观察
  612. */
  613. -(void)removeObserverToPlayerItem:(AVPlayerItem *)playerItem{
  614. [playerItem removeObserver:self forKeyPath:@"status"];
  615. [playerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
  616. [playerItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
  617. [playerItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
  618. }
  619. /*
  620. #pragma mark - Navigation
  621. // In a storyboard-based application, you will often want to do a little preparation before navigation
  622. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  623. // Get the new view controller using [segue destinationViewController].
  624. // Pass the selected object to the new view controller.
  625. }
  626. */
  627. @end