JCHATPhotoModel.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // HMThumbImageModel.m
  3. // photosFramework
  4. //
  5. // Created by HuminiOS on 15/11/11.
  6. // Copyright © 2015年 HuminiOS. All rights reserved.
  7. //
  8. #import "JCHATPhotoModel.h"
  9. @implementation JCHATPhotoModel
  10. - (instancetype)init
  11. {
  12. self = [super init];
  13. if (self) {
  14. _isSelected = NO;
  15. _isOriginPhoto = NO;
  16. }
  17. return self;
  18. }
  19. - (void)setDatawithAsset:(ALAsset *)asset {
  20. _asset = asset;
  21. _imgURL = asset.defaultRepresentation.url;
  22. CGSize imgSize;
  23. if (_isOriginPhoto) {
  24. } else {
  25. }
  26. }
  27. - (void)setDataWithPhotoAsset:(PHAsset *)asset imageManager:(PHCachingImageManager *)imageManager {
  28. _cachingManager = imageManager;
  29. _photoAsset = asset;
  30. CGSize imgSize;
  31. if (_isOriginPhoto) {
  32. imgSize = CGSizeMake(asset.pixelWidth, asset.pixelHeight);
  33. } else {
  34. imgSize = [[UIScreen mainScreen] bounds].size;
  35. CGFloat imgScale = [[UIScreen mainScreen] scale];
  36. imgSize = CGSizeMake(imgSize.width * imgScale, imgSize.height * imgScale);
  37. }
  38. _largeImageSize = imgSize;
  39. }
  40. - (void)setIsSelected:(BOOL)isSelected {
  41. _isSelected = isSelected;
  42. if (isSelected) {
  43. if (_asset == nil) {
  44. CGSize originSize = CGSizeMake(_photoAsset.pixelHeight/2, _photoAsset.pixelWidth/2);//改一下 720 width
  45. _largeImageSize = originSize;
  46. PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
  47. options.synchronous = YES;
  48. [_cachingManager requestImageForAsset:_photoAsset
  49. targetSize:[self fetchImageSizeWithOriginSize:originSize]
  50. contentMode:PHImageContentModeAspectFill
  51. options:options
  52. resultHandler:^(UIImage *result, NSDictionary *info) {
  53. _largeImage = result;
  54. }];//rename
  55. } else {
  56. [[[ALAssetsLibrary alloc]init] assetForURL:_imgURL resultBlock:^(ALAsset *asset) {
  57. _largeImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
  58. }failureBlock:^(NSError *error) {
  59. NSLog(@"error=%@",error);
  60. }];
  61. }
  62. } else {
  63. _largeImage = nil;
  64. }
  65. }
  66. - (void)setIsOriginPhoto:(BOOL)isOriginPhoto {
  67. _isOriginPhoto = isOriginPhoto;
  68. if (_isSelected) {
  69. if (_asset == nil) {
  70. CGSize originSize = CGSizeMake(_photoAsset.pixelWidth, _photoAsset.pixelHeight);
  71. _largeImageSize = originSize;
  72. PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
  73. options.synchronous = YES;
  74. [_cachingManager requestImageForAsset:_photoAsset
  75. targetSize:[self fetchImageSizeWithOriginSize:originSize]//imgSize
  76. contentMode:PHImageContentModeDefault
  77. options:options
  78. resultHandler:^(UIImage *result, NSDictionary *info) {
  79. _largeImage = result;
  80. }];
  81. }
  82. }
  83. }
  84. - (CGSize)fetchImageSizeWithOriginSize:(CGSize)originSize {
  85. CGFloat scale = originSize.width/originSize.height;
  86. CGSize fetchSize;
  87. if (scale > 1) {//长图
  88. fetchSize = CGSizeMake(1280, 1280 / scale );
  89. } else {
  90. fetchSize = CGSizeMake(720 * scale, 720);
  91. }
  92. return fetchSize;
  93. }
  94. @end