RITLPhotosPreviewController.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // YPPhotoPreviewController.m
  3. // YPPhotoDemo
  4. //
  5. // Created by YueWen on 16/8/5.
  6. // Copyright © 2017年 YueWen. All rights reserved.
  7. //
  8. #import "RITLPhotosPreviewController.h"
  9. #import <Photos/Photos.h>
  10. #import "RITLKit.h"
  11. #import "Masonry.h"
  12. @interface RITLPhotosPreviewController ()
  13. @property (nonatomic, strong)PHCachingImageManager *imageManager;
  14. @property (nonatomic, strong)UIImageView *imageView;
  15. @end
  16. @implementation RITLPhotosPreviewController
  17. - (instancetype)init
  18. {
  19. if (self = [super init]) {
  20. self.imageManager = PHCachingImageManager.new;
  21. }
  22. return self;
  23. }
  24. -(instancetype)initWithShowAsset:(PHAsset *)showAsset
  25. {
  26. if (self = [self init]){
  27. _showAsset = showAsset;
  28. }
  29. return self;
  30. }
  31. +(instancetype)previewWithShowAsset:(PHAsset *)showAsset
  32. {
  33. return [[self alloc]initWithShowAsset:showAsset];
  34. }
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view.
  38. //获得图片的宽度与高度的比例
  39. CGFloat scale = self.showAsset.pixelHeight * 1.0 / self.showAsset.pixelWidth;
  40. CGSize assetSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.width * scale);
  41. //设置当前的大小
  42. self.preferredContentSize = assetSize;
  43. //进行约束布局
  44. self.imageView = [UIImageView new];
  45. [self.view addSubview:self.imageView];
  46. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.edges.equalTo(self.view);
  48. }];
  49. self.imageView.backgroundColor = [UIColor whiteColor];
  50. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  51. self.imageView.layer.cornerRadius = 8;
  52. self.imageView.clipsToBounds = true;
  53. [self.imageManager requestImageForAsset:self.showAsset targetSize:@[@(self.showAsset.pixelWidth),@(self.showAsset.pixelHeight)].ritl_size contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
  54. self.imageView.image = result;
  55. }];
  56. }
  57. - (void)didReceiveMemoryWarning {
  58. [super didReceiveMemoryWarning];
  59. // Dispose of any resources that can be recreated.
  60. }
  61. -(void)dealloc
  62. {
  63. self.imageView.image = nil;
  64. NSLog(@"%@ is dealloc!",NSStringFromClass(self.class));
  65. }
  66. @end