DKPhotoPickerDatas.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // PickerDatas.m
  3. // 相册Demo
  4. //
  5. //
  6. #import "DKPhotoPickerDatas.h"
  7. #import "DKPhotoPickerGroup.h"
  8. #import <AssetsLibrary/AssetsLibrary.h>
  9. @interface DKPhotoPickerDatas ()
  10. @property (nonatomic , strong) ALAssetsLibrary *library;
  11. @end
  12. @implementation DKPhotoPickerDatas
  13. + (ALAssetsLibrary *)defaultAssetsLibrary
  14. {
  15. static dispatch_once_t pred = 0;
  16. static ALAssetsLibrary *library = nil;
  17. dispatch_once(&pred,^
  18. {
  19. library = [[ALAssetsLibrary alloc] init];
  20. });
  21. return library;
  22. }
  23. - (ALAssetsLibrary *)library
  24. {
  25. if (nil == _library)
  26. {
  27. _library = [self.class defaultAssetsLibrary];
  28. }
  29. return _library;
  30. }
  31. #pragma mark - getter
  32. + (instancetype) defaultPicker{
  33. return [[self alloc] init];
  34. }
  35. #pragma mark -获取所有组
  36. - (void) getAllGroupWithPhotos : (callBackBlock ) callBack{
  37. [self getAllGroupAllPhotos:YES withResource:callBack];
  38. }
  39. - (void) getAllGroupAllPhotos:(BOOL)allPhotos withResource : (callBackBlock ) callBack{
  40. NSMutableArray *groups = [NSMutableArray array];
  41. ALAssetsLibraryGroupsEnumerationResultsBlock resultBlock = ^(ALAssetsGroup *group, BOOL *stop){
  42. if (group) {
  43. if (allPhotos){
  44. [group setAssetsFilter:[ALAssetsFilter allPhotos]];
  45. }else{
  46. [group setAssetsFilter:[ALAssetsFilter allVideos]];
  47. }
  48. // 包装一个模型来赋值
  49. DKPhotoPickerGroup *pickerGroup = [[DKPhotoPickerGroup alloc] init];
  50. pickerGroup.group = group;
  51. pickerGroup.groupName = [group valueForProperty:@"ALAssetsGroupPropertyName"];
  52. pickerGroup.thumbImage = [UIImage imageWithCGImage:[group posterImage]];
  53. pickerGroup.assetsCount = [group numberOfAssets];
  54. [groups addObject:pickerGroup];
  55. }else{
  56. callBack(groups);
  57. }
  58. };
  59. NSInteger type = ALAssetsGroupAll;
  60. [self.library enumerateGroupsWithTypes:type usingBlock:resultBlock failureBlock:nil];
  61. }
  62. /**
  63. * 获取所有组对应的图片
  64. */
  65. - (void) getAllGroupWithVideos:(callBackBlock)callBack {
  66. [self getAllGroupAllPhotos:NO withResource:callBack];
  67. }
  68. #pragma mark -传入一个组获取组里面的Asset
  69. - (void) getGroupPhotosWithGroup : (DKPhotoPickerGroup *) pickerGroup finished : (callBackBlock ) callBack{
  70. NSMutableArray *assets = [NSMutableArray array];
  71. ALAssetsGroupEnumerationResultsBlock result = ^(ALAsset *asset , NSUInteger index , BOOL *stop){
  72. if (asset) {
  73. [assets addObject:asset];
  74. }else{
  75. callBack(assets);
  76. }
  77. };
  78. [pickerGroup.group enumerateAssetsUsingBlock:result];
  79. }
  80. #pragma mark -传入一个AssetsURL来获取UIImage
  81. - (void) getAssetsPhotoWithURLs:(NSURL *) url callBack:(callBackBlock ) callBack{
  82. [self.library assetForURL:url resultBlock:^(ALAsset *asset) {
  83. dispatch_async(dispatch_get_main_queue(), ^{
  84. callBack([UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]);
  85. });
  86. } failureBlock:nil];
  87. }
  88. #pragma mark - 通过传入一个图片对象(ALAsset、URL)获取一张缩略图
  89. - (UIImage *) getImageWithImageObj:(id)imageObj{
  90. __block UIImage *image = nil;
  91. if ([imageObj isKindOfClass:[UIImage class]]) {
  92. image = imageObj;
  93. }else if ([imageObj isKindOfClass:[ALAsset class]]){
  94. ALAsset *asset = (ALAsset *)imageObj;
  95. image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
  96. }else if ([imageObj isKindOfClass:[NSURL class]]){
  97. image = imageObj;
  98. }
  99. return image;
  100. }
  101. @end