2
0

DNAlbum.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // DNAlbum.m
  3. // DNImagePicker
  4. //
  5. // Created by Ding Xiao on 16/7/6.
  6. // Copyright © 2016年 Dennis. All rights reserved.
  7. //
  8. #import <Photos/Photos.h>
  9. #import "DNAlbum.h"
  10. #import "DNImagePickerHelper.h"
  11. #import "DNAsset.h"
  12. @interface DNAlbum ()
  13. @property (nonatomic, strong) NSAttributedString *albumAttributedString;
  14. @end
  15. @implementation DNAlbum
  16. - (instancetype)init {
  17. self = [super init];
  18. if (self) {
  19. _albumTitle = @"";
  20. _identifier = @"";
  21. _count = 0;
  22. }
  23. return self;
  24. }
  25. + (DNAlbum *)albumWithAssetCollection:(PHAssetCollection *)collection results:(PHFetchResult *)results{
  26. DNAlbum *album = [[DNAlbum alloc] init];
  27. if (!collection || !results) {
  28. return album;
  29. }
  30. album.count = results.count;
  31. album.results = results;
  32. album.albumTitle = collection.localizedTitle;
  33. album.identifier = collection.localIdentifier;
  34. return album;
  35. }
  36. - (void)fetchPostImageWithSize:(CGSize)size
  37. imageResutHandler:(void (^)(UIImage *))handler {
  38. [DNImagePickerHelper fetchImageWithAsset:[DNAsset assetWithPHAsset:self.results.lastObject]
  39. targetSize:size
  40. imageResutHandler:^(UIImage *postImage) {
  41. handler(postImage);
  42. }];
  43. }
  44. - (NSAttributedString *)albumAttributedString {
  45. if (!_albumAttributedString) {
  46. NSString *numberString = [NSString stringWithFormat:@" (%@)",@(self.count)];
  47. NSString *cellTitleString = [NSString stringWithFormat:@"%@%@",self.albumTitle,numberString];
  48. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:cellTitleString];
  49. [attributedString setAttributes: @{
  50. NSFontAttributeName : [UIFont systemFontOfSize:16.0f],
  51. NSForegroundColorAttributeName : [UIColor blackColor],
  52. }
  53. range:NSMakeRange(0, self.albumTitle.length)];
  54. [attributedString setAttributes:@{
  55. NSFontAttributeName : [UIFont systemFontOfSize:16.0f],
  56. NSForegroundColorAttributeName : [UIColor grayColor],
  57. } range:NSMakeRange(self.albumTitle.length, numberString.length)];
  58. _albumAttributedString = attributedString;
  59. }
  60. return _albumAttributedString;
  61. }
  62. @end