UICollectionViewCell+RITLPhotosAsset.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // UICollectionViewCell+RITLPhotosAsset.m
  3. // RITLPhotoDemo
  4. //
  5. // Created by YueWen on 2018/5/9.
  6. // Copyright © 2018年 YueWen. All rights reserved.
  7. //
  8. #import "UICollectionViewCell+RITLPhotosAsset.h"
  9. #import "RITLPhotosBrowseImageCell.h"
  10. #import "RITLPhotosBrowseLiveCell.h"
  11. #import "RITLPhotosBrowseVideoCell.h"
  12. #import <PhotosUI/PhotosUI.h>
  13. #import "Masonry.h"
  14. #import <objc/runtime.h>
  15. #import "RITLKit.h"
  16. NSNotificationName RITLHorBrowseTooBarChangedHiddenStateNotification = @"RITLHorBrowseTooBarChangedHiddenStateNSNotification";
  17. @implementation UICollectionViewCell (RITLPhotosAsset)
  18. - (NSString *)representedAssetIdentifier
  19. {
  20. return objc_getAssociatedObject(self, _cmd);
  21. }
  22. - (void)setRepresentedAssetIdentifier:(NSString *)representedAssetIdentifier
  23. {
  24. objc_setAssociatedObject(self, @selector(representedAssetIdentifier), representedAssetIdentifier, OBJC_ASSOCIATION_COPY_NONATOMIC);
  25. }
  26. - (PHAsset *)currentAsset
  27. {
  28. return objc_getAssociatedObject(self, _cmd);
  29. }
  30. - (void)setCurrentAsset:(PHAsset *)currentAsset
  31. {
  32. objc_setAssociatedObject(self, @selector(currentAsset), currentAsset, OBJC_ASSOCIATION_ASSIGN);
  33. }
  34. /// 默认不做任何事情
  35. - (void)updateAssets:(PHAsset *)asset
  36. atIndexPath:(nonnull NSIndexPath *)indexPath
  37. imageManager:(PHCachingImageManager *)cacheManager
  38. {
  39. self.representedAssetIdentifier = asset.localIdentifier;
  40. self.currentAsset = asset;
  41. PHImageRequestOptions *options = PHImageRequestOptions.new;
  42. options.networkAccessAllowed = true;
  43. [cacheManager requestImageForAsset:asset targetSize:@[@(asset.pixelWidth),@(asset.pixelHeight)].ritl_size contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
  44. //如果标志位一样
  45. if ([self.representedAssetIdentifier isEqualToString:asset.localIdentifier]) {//进行赋值操作
  46. [self updateViews:result info:info];
  47. }
  48. }];
  49. }
  50. - (void)playerAsset { }
  51. - (void)stop { }
  52. - (void)reset {}
  53. - (void)updateViews:(UIImage *)image info:(NSDictionary *)info { }
  54. @end
  55. @implementation RITLPhotosBrowseImageCell (RITLPhotosAsset)
  56. - (void)updateViews:(UIImage *)image info:(NSDictionary *)info
  57. {
  58. self.imageView.image = image;
  59. }
  60. - (void)reset
  61. {
  62. [self.bottomScrollView setZoomScale:1.0];
  63. }
  64. @end
  65. @implementation RITLPhotosBrowseLiveCell (RITLPhotosAsset)
  66. - (void)updateViews:(UIImage *)image info:(NSDictionary *)info
  67. {
  68. self.imageView.image = image;
  69. //设置
  70. [self.livePhotoView mas_remakeConstraints:^(MASConstraintMaker *make) {
  71. make.center.equalTo(self.imageView);
  72. make.width.equalTo(self.imageView);
  73. make.height.mas_equalTo(RITL_SCREEN_WIDTH * self.currentAsset.pixelHeight / self.currentAsset.pixelWidth);
  74. }];
  75. }
  76. - (void)playerAsset
  77. {
  78. if (!self.currentAsset || self.currentAsset.mediaSubtypes != PHAssetMediaSubtypePhotoLive) { return; }
  79. [self layoutIfNeeded];//重新布局
  80. //请求播放
  81. PHLivePhotoRequestOptions *options = PHLivePhotoRequestOptions.new;
  82. options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
  83. options.networkAccessAllowed = true;
  84. [PHImageManager.defaultManager requestLivePhotoForAsset:self.currentAsset targetSize:@[@(self.currentAsset.pixelWidth),@(self.currentAsset.pixelHeight)].ritl_size contentMode:PHImageContentModeAspectFill options:options resultHandler:^(PHLivePhoto * _Nullable livePhoto, NSDictionary * _Nullable info) {
  85. if (!livePhoto) { return; }
  86. self.livePhotoView.livePhoto = livePhoto;
  87. if (!self.isPlaying) {
  88. [self.livePhotoView startPlaybackWithStyle:PHLivePhotoViewPlaybackStyleHint];
  89. }
  90. }];
  91. }
  92. - (void)stop
  93. {
  94. if (self.isPlaying) {
  95. [self.livePhotoView stopPlayback];
  96. }
  97. }
  98. @end
  99. @implementation RITLPhotosBrowseVideoCell (RITLPhotosAsset)
  100. - (void)updateViews:(UIImage *)image info:(NSDictionary *)info
  101. {
  102. self.imageView.image = image;
  103. }
  104. - (void)playerAsset
  105. {
  106. if (!self.currentAsset || self.currentAsset.mediaType != PHAssetMediaTypeVideo) { return; }
  107. if (self.playerLayer && self.playerLayer.player) {//直接播放即可
  108. //进行对比
  109. [self.playerLayer.player play]; return;
  110. }
  111. PHVideoRequestOptions *options = PHVideoRequestOptions.new;
  112. options.networkAccessAllowed = true;
  113. options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
  114. //开始请求
  115. [PHImageManager.defaultManager requestPlayerItemForVideo:self.currentAsset options:options resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
  116. if (!playerItem) { return; }
  117. dispatch_sync(dispatch_get_main_queue(), ^{
  118. AVPlayer *player = [[AVPlayer alloc]initWithPlayerItem:playerItem];
  119. self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
  120. //Notication
  121. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(stop) name:AVPlayerItemDidPlayToEndTimeNotification object:player.currentItem];
  122. //config
  123. self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
  124. self.playerLayer.frame = self.imageView.layer.bounds;
  125. self.playImageView.hidden = true;
  126. [self.imageView.layer addSublayer:self.playerLayer];
  127. [[NSNotificationCenter defaultCenter]postNotificationName:RITLHorBrowseTooBarChangedHiddenStateNotification object:self userInfo:@{@"hidden":@(true)}];
  128. [player play];//播放
  129. });
  130. }];
  131. }
  132. - (void)stop
  133. {
  134. if (self.playerLayer && self.playerLayer.player) {
  135. [[NSNotificationCenter defaultCenter]postNotificationName:RITLHorBrowseTooBarChangedHiddenStateNotification object:nil userInfo:@{@"hidden":@(false)}];
  136. [self.playerLayer.player pause];;
  137. [self.playerLayer removeFromSuperlayer];//移除
  138. [NSNotificationCenter.defaultCenter removeObserver:self];
  139. self.playImageView.hidden = false;
  140. self.playerLayer = nil;
  141. }
  142. }
  143. @end