WFCUImagePreviewViewController.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // ImagePreviewViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/8.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUImagePreviewViewController.h"
  9. #import "SDWebImage.h"
  10. @interface WFCUImagePreviewViewController ()
  11. @property (nonatomic, strong)UIScrollView *scrollView;
  12. @property (nonatomic, strong)UIImageView *imageView;
  13. @end
  14. @implementation WFCUImagePreviewViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  18. [self.view addSubview:_scrollView];
  19. _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _thumbnail.size.width, _thumbnail.size.height)];
  20. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  21. [_scrollView addSubview:_imageView];
  22. __weak typeof(self) weakSelf = self;
  23. if ([_imageUrl rangeOfString:@"http"].location == 0 || [_imageUrl rangeOfString:@"ftp"].location == 0) {
  24. [_imageView sd_setImageWithURL:[NSURL URLWithString:_imageUrl] placeholderImage:_thumbnail completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  25. dispatch_async(dispatch_get_main_queue(), ^{
  26. [UIView animateWithDuration:0.3 animations:^{
  27. weakSelf.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
  28. weakSelf.scrollView.contentSize = weakSelf.imageView.image.size;
  29. }];
  30. });
  31. }];
  32. } else {
  33. _imageView.image = _thumbnail;
  34. weakSelf.imageView.frame = CGRectMake(0, 0, _imageView.image.size.width, _imageView.image.size.height);
  35. weakSelf.scrollView.contentSize = weakSelf.imageView.image.size;
  36. dispatch_async(dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_DEFAULT), ^{
  37. UIImage *image = [UIImage imageWithContentsOfFile:weakSelf.imageUrl];
  38. dispatch_async(dispatch_get_main_queue(), ^{
  39. [UIView animateWithDuration:0.3 animations:^{
  40. weakSelf.imageView.image = image;
  41. weakSelf.imageView.frame = CGRectMake(0, 0, weakSelf.imageView.image.size.width, weakSelf.imageView.image.size.height);
  42. weakSelf.scrollView.contentSize = weakSelf.imageView.image.size;
  43. }];
  44. });
  45. });
  46. }
  47. _scrollView.showsHorizontalScrollIndicator = NO;
  48. _scrollView.showsVerticalScrollIndicator = NO;
  49. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClose:)];
  50. tap.numberOfTapsRequired = 1;
  51. UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resize:)];
  52. tap2.numberOfTapsRequired = 2;
  53. [tap requireGestureRecognizerToFail:tap2];
  54. [self.imageView addGestureRecognizer:tap2];
  55. [self.imageView addGestureRecognizer:tap];
  56. self.imageView.userInteractionEnabled = YES;
  57. }
  58. - (void)resize:(id)sender {
  59. __weak typeof(self) weakSelf = self;
  60. [UIView animateWithDuration:0.3 animations:^{
  61. if(weakSelf.scrollView.contentSize.width == weakSelf.view.bounds.size.width) {
  62. weakSelf.scrollView.contentSize = weakSelf.imageView.image.size;
  63. weakSelf.scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
  64. weakSelf.imageView.frame = CGRectMake(0, 0, weakSelf.imageView.image.size.width, weakSelf.imageView.image.size.height);
  65. } else {
  66. weakSelf.scrollView.contentSize = weakSelf.view.bounds.size;
  67. CGRect frame = weakSelf.scrollView.frame;
  68. weakSelf.imageView.frame = frame;
  69. weakSelf.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
  70. }
  71. }];
  72. }
  73. - (void)onClose:(id)sender {
  74. [self dismissViewControllerAnimated:YES completion:nil];
  75. }
  76. - (void)didReceiveMemoryWarning {
  77. [super didReceiveMemoryWarning];
  78. // Dispose of any resources that can be recreated.
  79. }
  80. /*
  81. #pragma mark - Navigation
  82. // In a storyboard-based application, you will often want to do a little preparation before navigation
  83. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  84. // Get the new view controller using [segue destinationViewController].
  85. // Pass the selected object to the new view controller.
  86. }
  87. */
  88. @end