// // RQExerciseQuestionCell.m // SDJK // // Created by 张嵘 on 2021/8/16. // #import "RQExerciseQuestionCell.h" @interface RQExerciseQuestionCell () @property (nonatomic, readwrite, strong) RQExerciseQuestionItemViewModel *viewModel; @property (weak, nonatomic) IBOutlet YYLabel *questionLabel; @property (weak, nonatomic) IBOutlet UIImageView *questionImageView; @property (nonatomic, readwrite, strong) QMUIImagePreviewViewController *imagePreviewViewController; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelHeight; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight; @end @implementation RQExerciseQuestionCell #pragma mark - PublicMethods + (instancetype)cellWithTableView:(UITableView *)tableView { static NSString *ID = @"RQExerciseQuestionCell"; RQExerciseQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [self rq_viewFromXib]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } return cell; } - (void)dealloc { } - (void)bindViewModel:(RQExerciseQuestionItemViewModel *)viewModel { @weakify(self) _viewModel = viewModel; self.questionLabel.attributedText = viewModel.qusetionString; self.labelHeight.constant = viewModel.labelHeight; if (RQStringIsEmpty(viewModel.imageString)) { self.questionImageView.hidden = YES; } else { self.questionImageView.hidden = NO; self.imageViewHeight.constant = viewModel.imageHeight; self.questionImageView.qmui_smoothAnimation = YES; dispatch_async(dispatch_get_global_queue(0, 0), ^{ @strongify(self) [self.questionImageView yy_setImageWithURL:[NSURL URLWithString:viewModel.imageString] placeholder:RQWebImagePlaceholder() options:RQWebImageOptionAutomatic completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) { @strongify(self) if ([url.path containsString:@".gif"] ) { image = [UIImage qmui_animatedImageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_viewModel.imageString]]]; } //通知主线程刷新 dispatch_async(dispatch_get_main_queue(), ^{ @strongify(self) //回调或者说是通知主线程刷新 self.questionImageView.image = image; }); }]; }); // [self.questionImageView yy_setImageWithURL:[NSURL URLWithString:viewModel.imageString] placeholder:RQWebImagePlaceholder() options:RQWebImageOptionAutomatic completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) { // @strongify(self) // if ([url.path containsString:@".gif"] ) { // image = [UIImage qmui_animatedImageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_viewModel.imageString]]]; // } // dispatch_async(dispatch_get_main_queue(), ^{ // self.questionImageView.image = image; // }); // }]; [self.questionImageView setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) [RQControllerHelper.currentViewController presentViewController:self.imagePreviewViewController animated:YES completion:nil]; }]; } } #pragma mark - SystemMethods - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } #pragma mark - LazyLoad - (QMUIImagePreviewViewController *)imagePreviewViewController { if (!_imagePreviewViewController) { _imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init]; _imagePreviewViewController.presentingStyle = QMUIImagePreviewViewControllerTransitioningStyleZoom;// 将 present 动画改为 zoom,也即从某个位置放大到屏幕中央。默认样式为 fade。 _imagePreviewViewController.imagePreviewView.delegate = self;// 将内部的图片查看器 delegate 指向当前 viewController,以获取要查看的图片数据 // 当需要在退出大图预览时做一些事情的时候,可配合 UIViewController (QMUI) 的 qmui_visibleStateDidChangeBlock 来实现。 _imagePreviewViewController.qmui_visibleStateDidChangeBlock = ^(QMUIImagePreviewViewController *viewController, QMUIViewControllerVisibleState visibleState) { if (visibleState == QMUIViewControllerWillDisappear) { // NSInteger exitAtIndex = viewController.imagePreviewView.currentImageIndex; } }; } return _imagePreviewViewController; } #pragma mark - - (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView { return 1; } - (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index { zoomImageView.reusedIdentifier = @(index); // 模拟异步加载的情况 if (index == 2) { [zoomImageView showLoading]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if ([zoomImageView.reusedIdentifier isEqual:@(index)]) { [zoomImageView hideEmptyView]; zoomImageView.image = self.questionImageView.image; zoomImageView.contentMode = UIViewContentModeScaleAspectFill; zoomImageView.maximumZoomScale = 3.f; } }); } else { zoomImageView.image = self.questionImageView.image; } } - (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index { return QMUIImagePreviewMediaTypeImage; } #pragma mark - - (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location { // 退出图片预览 [RQControllerHelper.currentViewController dismissViewControllerAnimated:YES completion:^{ }]; } @end