123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- //
- // HXPhotoSubViewCell.m
- // HXPhotoPickerExample
- //
- // Created by Silence on 17/2/17.
- // Copyright © 2017年 Silence. All rights reserved.
- //
- #import "HXPhotoSubViewCell.h"
- #import "HXPhotoModel.h"
- #import "HXCircleProgressView.h"
- #import "HXPhotoTools.h"
- #import "HXPhotoBottomSelectView.h"
- #import "HXPhotoEdit.h"
- #import "UIColor+HXExtension.h"
- #import "HXAssetManager.h"
- @interface HXPhotoSubViewCell ()
- @property (strong, nonatomic) UIImageView *imageView;
- @property (strong, nonatomic) UIButton *deleteBtn;
- @property (strong, nonatomic) HXCircleProgressView *progressView;
- @property (assign, nonatomic) PHImageRequestID requestID;
- @property (strong, nonatomic) UILabel *stateLb;
- @property (strong, nonatomic) CAGradientLayer *bottomMaskLayer;
- @property (strong, nonatomic) UIView *bottomMaskView;
- @property (assign, nonatomic) BOOL addCustomViewCompletion;
- @property (strong, nonatomic) UIView *customView;
- @end
- @implementation HXPhotoSubViewCell
- - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
- [super traitCollectionDidChange:previousTraitCollection];
- #ifdef __IPHONE_13_0
- if (@available(iOS 13.0, *)) {
- if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
- if (self.model.type == HXPhotoModelMediaTypeCamera) {
- if ([HXPhotoCommon photoCommon].isDark) {
- self.imageView.image = self.model.previewPhoto;
- }else {
- self.imageView.image = self.model.thumbPhoto;
- }
- }
- }
- }
- #endif
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self setup];
- }
- return self;
- }
- #pragma mark - < 懒加载 >
- - (UIImageView *)imageView {
- if (!_imageView) {
- _imageView = [[UIImageView alloc] init];
- _imageView.clipsToBounds = YES;
- _imageView.contentMode = UIViewContentModeScaleAspectFill;
- }
- return _imageView;
- }
- - (UILabel *)stateLb {
- if (!_stateLb) {
- _stateLb = [[UILabel alloc] init];
- _stateLb.textColor = [UIColor whiteColor];
- _stateLb.textAlignment = NSTextAlignmentRight;
- _stateLb.font = [UIFont hx_mediumSFUITextOfSize:12];
- }
- return _stateLb;
- }
- - (UIView *)bottomMaskView {
- if (!_bottomMaskView) {
- _bottomMaskView = [[UIView alloc] init];
- [_bottomMaskView.layer addSublayer:self.bottomMaskLayer];
- }
- return _bottomMaskView;
- }
- - (CAGradientLayer *)bottomMaskLayer {
- if (!_bottomMaskLayer) {
- _bottomMaskLayer = [CAGradientLayer layer];
- _bottomMaskLayer.colors = @[
- (id)[[UIColor blackColor] colorWithAlphaComponent:0].CGColor ,
- (id)[[UIColor blackColor] colorWithAlphaComponent:0.15].CGColor ,
- (id)[[UIColor blackColor] colorWithAlphaComponent:0.35].CGColor ,
- (id)[[UIColor blackColor] colorWithAlphaComponent:0.6].CGColor
- ];
- _bottomMaskLayer.startPoint = CGPointMake(0, 0);
- _bottomMaskLayer.endPoint = CGPointMake(0, 1);
- _bottomMaskLayer.locations = @[@(0.15f),@(0.35f),@(0.6f),@(0.9f)];
- _bottomMaskLayer.borderWidth = 0.0;
- }
- return _bottomMaskLayer;
- }
- - (UIButton *)deleteBtn {
- if (!_deleteBtn) {
- _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_deleteBtn setImage:[UIImage hx_imageNamed:@"hx_compose_delete"] forState:UIControlStateNormal];
- [_deleteBtn addTarget:self action:@selector(didDeleteClick) forControlEvents:UIControlEventTouchUpInside];
- }
- return _deleteBtn;
- }
- - (HXCircleProgressView *)progressView {
- if (!_progressView) {
- _progressView = [[HXCircleProgressView alloc] init];
- _progressView.hidden = YES;
- }
- return _progressView;
- }
- - (void)setup {
- [self.contentView addSubview:self.imageView];
- [self.contentView addSubview:self.bottomMaskView];
- [self.contentView addSubview:self.stateLb];
- [self.contentView addSubview:self.deleteBtn];
- [self.contentView addSubview:self.progressView];
- [self.contentView addSubview:self.highlightMaskView];
- }
- - (void)didDeleteClick {
- BOOL showAlert = NO;
- NSString *title;
- if (self.deleteCellShowAlert) {
- showAlert = YES;
- if (self.model.subType == HXPhotoModelMediaSubTypePhoto) {
- title = [NSBundle hx_localizedStringForKey:@"要删除这张照片吗?"];
- }else if (self.model.subType == HXPhotoModelMediaSubTypeVideo) {
- title = [NSBundle hx_localizedStringForKey:@"要删除此视频吗?"];
- }
- }else {
- if (self.model.networkPhotoUrl) {
- if (self.showDeleteNetworkPhotoAlert) {
- showAlert = YES;
- title = [NSBundle hx_localizedStringForKey:@"是否删除此资源"];
- }
- }
- }
- if (showAlert) {
- HXPhotoBottomViewModel *titleModel = [[HXPhotoBottomViewModel alloc] init];
- titleModel.title = title ?: @"";
- titleModel.titleFont = [UIFont systemFontOfSize:13];
- titleModel.titleColor = [UIColor hx_colorWithHexStr:@"#666666"];
- titleModel.titleDarkColor = [UIColor hx_colorWithHexStr:@"#999999"];
- titleModel.cellHeight = 60.f;
- titleModel.canSelect = NO;
-
- HXPhotoBottomViewModel *deleteModel = [[HXPhotoBottomViewModel alloc] init];
- deleteModel.title = [NSBundle hx_localizedStringForKey:@"删除"];
- deleteModel.titleColor = [UIColor redColor];
- deleteModel.titleDarkColor = [[UIColor redColor] colorWithAlphaComponent:0.8f];
- HXWeakSelf
- [HXPhotoBottomSelectView showSelectViewWithModels:@[titleModel, deleteModel] selectCompletion:^(NSInteger index, HXPhotoBottomViewModel * _Nonnull model) {
- if ([weakSelf.delegate respondsToSelector:@selector(cellDidDeleteClcik:)]) {
- [weakSelf.delegate cellDidDeleteClcik:weakSelf];
- }
- } cancelClick:nil];
- return;
- }
- #if HasYYWebImage
- // [self.imageView yy_cancelCurrentImageRequest];
- #elif HasYYKit
- // [self.imageView cancelCurrentImageRequest];
- #elif HasSDWebImage
- // [self.imageView sd_cancelCurrentAnimationImagesLoad];
- #endif
- if ([self.delegate respondsToSelector:@selector(cellDidDeleteClcik:)]) {
- [self.delegate cellDidDeleteClcik:self];
- }
- }
- - (void)againDownload {
- self.model.downloadError = NO;
- self.model.downloadComplete = NO;
- HXWeakSelf
- [self.imageView hx_setImageWithModel:self.model original:NO progress:^(CGFloat progress, HXPhotoModel *model) {
- if (weakSelf.model == model) {
- weakSelf.progressView.progress = progress;
- }
- } completed:^(UIImage *image, NSError *error, HXPhotoModel *model) {
- if (weakSelf.model == model) {
- if (error != nil) {
- weakSelf.model.downloadError = YES;
- weakSelf.model.downloadComplete = YES;
- [weakSelf.progressView showError];
- }else {
- if (image) {
- weakSelf.progressView.progress = 1;
- weakSelf.progressView.hidden = YES;
- weakSelf.imageView.image = image;
- weakSelf.userInteractionEnabled = YES;
- }
- }
- }
- }];
- }
- - (void)setHideDeleteButton:(BOOL)hideDeleteButton {
- _hideDeleteButton = hideDeleteButton;
- if (self.model.type != HXPhotoModelMediaTypeCamera) {
- self.deleteBtn.hidden = hideDeleteButton;
- }
- }
- - (void)setDeleteImageName:(NSString *)deleteImageName {
- _deleteImageName = deleteImageName;
- [self.deleteBtn setImage:[UIImage hx_imageNamed:deleteImageName] forState:UIControlStateNormal];
- }
- - (void)resetNetworkImage {
- if (self.model.networkPhotoUrl &&
- (self.model.type == HXPhotoModelMediaTypeCameraPhoto ||
- self.model.cameraVideoType == HXPhotoModelMediaTypeCameraVideoTypeNetWork)) {
- self.model.loadOriginalImage = YES;
- self.model.previewViewSize = CGSizeZero;
- self.model.endImageSize = CGSizeZero;
- HXWeakSelf
- [self.imageView hx_setImageWithModel:self.model original:YES progress:nil completed:^(UIImage *image, NSError *error, HXPhotoModel *model) {
- if (weakSelf.model == model) {
- if (image.images.count) {
- weakSelf.imageView.image = nil;
- weakSelf.imageView.image = image.images.firstObject;
- }else {
- weakSelf.imageView.image = image;
- }
- }
- }];
- }
- }
- - (void)setModel:(HXPhotoModel *)model {
- _model = model;
- self.progressView.hidden = YES;
- self.progressView.progress = 0;
- self.imageView.image = nil;
- if (model.type == HXPhotoModelMediaTypeCamera) {
- self.deleteBtn.hidden = YES;
- if ([HXPhotoCommon photoCommon].isDark) {
- self.imageView.image = model.previewPhoto;
- }else {
- self.imageView.image = model.thumbPhoto;
- }
- }else {
- if (model.localIdentifier && !model.asset) {
- model.asset = [HXAssetManager fetchAssetWithLocalIdentifier:model.localIdentifier];
- }
- self.deleteBtn.hidden = NO;
- if (model.networkPhotoUrl) {
- if (model.photoEdit) {
- self.imageView.image = model.photoEdit.editPreviewImage;
- }else {
- HXWeakSelf
- if (model.downloadComplete && !model.downloadError) {
- if (model.previewPhoto.images.count) {
- self.imageView.image = nil;
- self.imageView.image = model.previewPhoto.images.firstObject;
- }else {
- self.imageView.image = model.previewPhoto;
- }
- }else {
- self.progressView.hidden = NO;
- [self.imageView hx_setImageWithModel:model original:NO progress:^(CGFloat progress, HXPhotoModel *model) {
- if (weakSelf.model == model) {
- weakSelf.progressView.progress = progress;
- }
- } completed:^(UIImage *image, NSError *error, HXPhotoModel *model) {
- if (weakSelf.model == model) {
- if (error != nil) {
- [weakSelf.progressView showError];
- }else {
- if (image) {
- weakSelf.progressView.progress = 1;
- weakSelf.progressView.hidden = YES;
- if (image.images.count) {
- weakSelf.imageView.image = nil;
- weakSelf.imageView.image = image.images.firstObject;
- }else {
- weakSelf.imageView.image = image;
- }
- }
- }
- }
- }];
- }
- }
- }else {
- if (model.photoEdit) {
- self.imageView.image = model.photoEdit.editPreviewImage;
- }else {
- if (model.asset) {
- HXWeakSelf
- PHImageRequestID requestID = [self.model requestThumbImageWithWidth:250 completion:^(UIImage *image, HXPhotoModel *model, NSDictionary *info) {
- if (weakSelf.model == model) {
- weakSelf.imageView.image = image;
- }
- }];
- if (self.requestID != requestID) {
- [[PHImageManager defaultManager] cancelImageRequest:self.requestID];
- }
- self.requestID = requestID;
- }else {
- if (model.previewPhoto) {
- if (model.previewPhoto.images.count) {
- self.imageView.image = nil;
- self.imageView.image = model.previewPhoto.images.firstObject;
- }else {
- self.imageView.image = model.previewPhoto;
- }
- }else if (model.thumbPhoto) {
- if (model.thumbPhoto.images.count) {
- self.imageView.image = nil;
- self.imageView.image = model.thumbPhoto.images.firstObject;
- }else {
- self.imageView.image = model.thumbPhoto;
- }
- }
- }
- }
- }
- }
- if (self.customProtocol) {
- [CATransaction begin];
- [CATransaction setDisableActions:YES];
- }
- if (model.type == HXPhotoModelMediaTypePhotoGif && !model.photoEdit) {
- self.stateLb.text = @"GIF";
- self.stateLb.hidden = NO;
- self.bottomMaskView.hidden = NO;
- }else if (model.type == HXPhotoModelMediaTypeLivePhoto && !model.photoEdit) {
- self.stateLb.text = @"Live";
- self.stateLb.hidden = NO;
- self.bottomMaskView.hidden = NO;
- }else {
- if (model.subType == HXPhotoModelMediaSubTypeVideo) {
- self.stateLb.text = model.videoTime;
- self.stateLb.hidden = NO;
- self.bottomMaskView.hidden = NO;
- }else {
- if ((model.cameraPhotoType == HXPhotoModelMediaTypeCameraPhotoTypeNetWorkGif ||
- model.cameraPhotoType == HXPhotoModelMediaTypeCameraPhotoTypeLocalGif) && !model.photoEdit) {
- self.stateLb.text = @"GIF";
- self.stateLb.hidden = NO;
- self.bottomMaskView.hidden = NO;
- return;
- }else if ((model.cameraPhotoType == HXPhotoModelMediaTypeCameraPhotoTypeLocalLivePhoto ||
- model.cameraPhotoType == HXPhotoModelMediaTypeCameraPhotoTypeNetWorkLivePhoto) &&
- !model.photoEdit) {
- self.stateLb.text = @"Live";
- self.stateLb.hidden = NO;
- self.bottomMaskView.hidden = NO;
- return;
- }
- self.stateLb.hidden = YES;
- self.bottomMaskView.hidden = YES;
- }
- }
- NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.index inSection:0];
- if ([self.customProtocol respondsToSelector:@selector(customView:indexPath:)]) {
- if (!self.addCustomViewCompletion) {
- UIView *customView = [self.customProtocol customView:self indexPath:indexPath];
- if (customView) {
- [self.contentView addSubview:customView];
- }
- self.customView = customView;
- self.addCustomViewCompletion = YES;
- }
- }
- if ([self.customProtocol respondsToSelector:@selector(setCustomViewData:cell:model:indexPath:)]) {
- [self.customProtocol setCustomViewData:self.customView cell:self model:model indexPath:indexPath];
- }
- if ([self.customProtocol respondsToSelector:@selector(shouldHiddenBottomType:indexPath:)]) {
- BOOL hiddenState = [self.customProtocol shouldHiddenBottomType:self indexPath:indexPath];
- if (hiddenState) {
- self.stateLb.hidden = hiddenState;
- self.bottomMaskView.hidden = hiddenState;
- }
- }
- if ([self.customProtocol respondsToSelector:@selector(customViewFrame:indexPath:)]) {
- CGRect customViewFrame = [self.customProtocol customViewFrame:self indexPath:indexPath];
- self.customView.frame = customViewFrame;
- }
- if ([self.customProtocol respondsToSelector:@selector(customDeleteButtonFrame:indexPath:)]) {
- CGRect deleteFrame = [self.customProtocol customDeleteButtonFrame:self indexPath:indexPath];
- self.deleteBtn.frame = deleteFrame;
- }
- if (self.customProtocol) {
- [CATransaction commit];
- }
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
- self.imageView.frame = self.bounds;
-
- self.stateLb.frame = CGRectMake(0, self.hx_h - 18, self.hx_w - 4, 18);
- self.bottomMaskView.frame = CGRectMake(0, self.hx_h - 25, self.hx_w, 25);
- self.bottomMaskLayer.frame = self.bottomMaskView.bounds;
- CGFloat width = self.frame.size.width;
- CGFloat height = self.frame.size.height;
- if (![self.customProtocol respondsToSelector:@selector(customDeleteButtonFrame:indexPath:)]) {
- CGFloat deleteBtnW = self.deleteBtn.currentImage.size.width;
- CGFloat deleteBtnH = self.deleteBtn.currentImage.size.height;
- self.deleteBtn.frame = CGRectMake(width - deleteBtnW, 0, deleteBtnW, deleteBtnH);
- }
-
- self.progressView.center = CGPointMake(width / 2, height / 2);
- self.highlightMaskView.frame = self.bounds;
- }
- - (void)setHighlighted:(BOOL)highlighted {
- [super setHighlighted:highlighted];
- if (self.model.type == HXPhotoModelMediaTypeCamera || self.canEdit) {
- return;
- }
- self.highlightMaskView.hidden = !highlighted;
- }
- - (UIView *)highlightMaskView {
- if (!_highlightMaskView) {
- _highlightMaskView = [[UIView alloc] init];
- _highlightMaskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
- _highlightMaskView.hidden = YES;
- }
- return _highlightMaskView;
- }
- @end
|