猎豆优选

ZLPhotoManager.h 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // ZLPhotoManager.h
  3. // ZLPhotoBrowser
  4. //
  5. // Created by long on 17/4/12.
  6. // Copyright © 2017年 long. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <Photos/Photos.h>
  10. #import "ZLPhotoModel.h"
  11. #import "ZLDefine.h"
  12. @class ZLAlbumListModel;
  13. @interface ZLPhotoManager : NSObject
  14. /**
  15. * @brief 设置排序模式
  16. */
  17. + (void)setSortAscending:(BOOL)ascending;
  18. /**
  19. * @brief 保存图片到系统相册
  20. */
  21. + (void)saveImageToAblum:(UIImage *)image completion:(void (^)(BOOL suc, PHAsset *asset))completion;
  22. /**
  23. * @brief 保存视频到系统相册
  24. */
  25. + (void)saveVideoToAblum:(NSURL *)url completion:(void (^)(BOOL suc, PHAsset *asset))completion;
  26. /**
  27. * @brief 在全部照片中获取指定个数、排序方式的部分照片,在跳往预览大图界面时候video和gif均为no,不受参数影响
  28. */
  29. + (NSArray<ZLPhotoModel *> *)getAllAssetInPhotoAlbumWithAscending:(BOOL)ascending limitCount:(NSInteger)limit allowSelectVideo:(BOOL)allowSelectVideo allowSelectImage:(BOOL)allowSelectImage allowSelectGif:(BOOL)allowSelectGif allowSelectLivePhoto:(BOOL)allowSelectLivePhoto;
  30. /**
  31. * @brief 获取相机胶卷相册列表对象
  32. */
  33. + (ZLAlbumListModel *)getCameraRollAlbumList:(BOOL)allowSelectVideo allowSelectImage:(BOOL)allowSelectImage;
  34. /**
  35. block 获取相机胶卷相册列表对象
  36. */
  37. + (void)getCameraRollAlbumList:(BOOL)allowSelectVideo allowSelectImage:(BOOL)allowSelectImage complete:(void (^)(ZLAlbumListModel *album))complete;
  38. /**
  39. * @brief 获取用户所有相册列表
  40. */
  41. + (void)getPhotoAblumList:(BOOL)allowSelectVideo allowSelectImage:(BOOL)allowSelectImage complete:(void (^)(NSArray<ZLAlbumListModel *> *))complete;
  42. /**
  43. * @brief 将result中对象转换成ZLPhotoModel
  44. */
  45. + (NSArray<ZLPhotoModel *> *)getPhotoInResult:(PHFetchResult<PHAsset *> *)result allowSelectVideo:(BOOL)allowSelectVideo allowSelectImage:(BOOL)allowSelectImage allowSelectGif:(BOOL)allowSelectGif allowSelectLivePhoto:(BOOL)allowSelectLivePhoto;
  46. /**
  47. * @brief 获取选中的图片
  48. */
  49. + (void)requestSelectedImageForAsset:(ZLPhotoModel *)model isOriginal:(BOOL)isOriginal allowSelectGif:(BOOL)allowSelectGif completion:(void (^)(UIImage *, NSDictionary *))completion;
  50. /**
  51. 获取原图data,转换gif图
  52. */
  53. + (void)requestOriginalImageDataForAsset:(PHAsset *)asset completion:(void (^)(NSData *, NSDictionary *))completion;
  54. /**
  55. * @brief 获取原图
  56. */
  57. + (void)requestOriginalImageForAsset:(PHAsset *)asset completion:(void (^)(UIImage *, NSDictionary *))completion;
  58. /**
  59. * @brief 根据传入size获取图片
  60. */
  61. + (PHImageRequestID)requestImageForAsset:(PHAsset *)asset size:(CGSize)size completion:(void (^)(UIImage *, NSDictionary *))completion;
  62. /**
  63. * @brief 获取live photo
  64. */
  65. + (void)requestLivePhotoForAsset:(PHAsset *)asset completion:(void (^)(PHLivePhoto *livePhoto, NSDictionary *info))completion;
  66. /**
  67. * @brief 获取视频
  68. */
  69. + (void)requestVideoForAsset:(PHAsset *)asset completion:(void (^)(AVPlayerItem *item, NSDictionary *info))completion;
  70. #pragma mark - 逐个解析asset方法
  71. /**
  72. 自行解析图片方法
  73. 使用顺序单个解析,缓解了框架同时解析大量图片造成的内存暴涨
  74. 如果一下选择20张及以上照片(原图)建议使用自行解析
  75. 请求到图片后做了一个大小的压缩(原图时并未压缩尺寸)来缓解内存的占用
  76. */
  77. + (void)anialysisAssets:(NSArray<PHAsset *> *)assets original:(BOOL)original completion:(void (^)(NSArray<UIImage *> *images))completion;
  78. /**
  79. @brief 缩放图片
  80. */
  81. + (UIImage *)scaleImage:(UIImage *)image original:(BOOL)original;
  82. /**
  83. * @brief 将系统mediatype转换为自定义mediatype
  84. */
  85. + (ZLAssetMediaType)transformAssetType:(PHAsset *)asset;
  86. /**
  87. * @brief 转换视频时长
  88. */
  89. + (NSString *)getDuration:(PHAsset *)asset;
  90. /**
  91. * @brief 判断图片是否存储在本地/或者已经从iCloud上下载到本地
  92. */
  93. + (BOOL)judgeAssetisInLocalAblum:(PHAsset *)asset;
  94. /**
  95. * @brief 获取图片字节大小
  96. */
  97. + (void)getPhotosBytesWithArray:(NSArray<ZLPhotoModel *> *)photos completion:(void (^)(NSString *photosBytes))completion;
  98. /**
  99. * @brief 标记源数组中已被选择的model
  100. */
  101. + (void)markSelectModelInArr:(NSArray<ZLPhotoModel *> *)dataArr selArr:(NSArray<ZLPhotoModel *> *)selArr;
  102. /**
  103. * @brief 将image data转换为gif图片,sdwebimage
  104. */
  105. + (UIImage *)transformToGifImageWithData:(NSData *)data;
  106. /**
  107. 获取对应asset的路径
  108. */
  109. + (void)requestAssetFileUrl:(PHAsset *)asset complete:(void (^)(NSString *filePath))complete;
  110. #pragma mark - 编辑、导出视频相关
  111. /**
  112. 解析视频,获取每秒对应的一帧图片
  113. @param size 图片size
  114. */
  115. + (void)analysisEverySecondsImageForAsset:(PHAsset *)asset interval:(NSTimeInterval)interval size:(CGSize)size complete:(void (^)(AVAsset *avAsset, NSArray<UIImage *> *images))complete;
  116. /**
  117. 导出编辑的片段视频并保存到相册
  118. @param range 需要到处的视频间隔
  119. */
  120. + (void)exportEditVideoForAsset:(AVAsset *)asset range:(CMTimeRange)range type:(ZLExportVideoType)type complete:(void (^)(BOOL isSuc, PHAsset *asset))complete;
  121. /**
  122. 导出视频,视频压缩设置默认为 AVAssetExportPresetMediumQuality
  123. @param asset 需要导出视频的asset
  124. @param type 视频导出格式
  125. */
  126. + (void)exportVideoForAsset:(PHAsset *)asset type:(ZLExportVideoType)type complete:(void (^)(NSString *exportFilePath, NSError *error))complete;
  127. /**
  128. 导出视频
  129. @param asset 需要导出视频的asset
  130. @param type 视频导出格式
  131. @param presetName 视频压缩设置
  132. */
  133. + (void)exportVideoForAsset:(PHAsset *)asset type:(ZLExportVideoType)type presetName:(NSString *)presetName complete:(void (^)(NSString *exportFilePath, NSError *error))complete;
  134. /**
  135. 导出指定尺寸的视频,视频区域为以视频中心为中点(视频质量未压缩)
  136. @param asset 需要导出视频的asset
  137. @param type 视频导出格式
  138. @param renderSize 指定的尺寸大小
  139. */
  140. + (void)exportVideoForAsset:(PHAsset *)asset type:(ZLExportVideoType)type renderSize:(CGSize)renderSize complete:(void (^)(NSString *exportFilePath, NSError *error))complete;
  141. #pragma mark - 导出视频加水印、粒子效果
  142. /**
  143. 导出指定尺寸视频,并添加水印,视频区域为以视频中心为中点(视频质量未压缩)
  144. @discussion(由于文字水印在开发过程中遇到对同一个视频导出时候,有的文字显示,有的不显示的文字,所以暂不支持文字水印)
  145. @param asset 需要导出视频的asset
  146. @param type 视频导出格式
  147. @param renderSize 指定的尺寸大小,如要导出全尺寸视频,可将该值设置的大些如:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
  148. @param watermarkImage 水印图片
  149. @param location 水印位置
  150. @param imageSize 水印大小
  151. */
  152. + (void)exportVideoForAsset:(PHAsset *)asset type:(ZLExportVideoType)type renderSize:(CGSize)renderSize watermarkImage:(UIImage *)watermarkImage watermarkLocation:(ZLWatermarkLocation)location imageSize:(CGSize)imageSize complete:(void (^)(NSString *exportFilePath, NSError *error))complete;
  153. /**
  154. 导出全尺寸视频,并添加水印(支持设置压缩系数)
  155. @param asset 需要导出视频的asset
  156. @param type 视频导出格式
  157. @param presetName 视频压缩设置
  158. @param watermarkImage 水印图片
  159. @param location 水印位置
  160. @param imageSize 水印大小
  161. */
  162. + (void)exportVideoForAsset:(PHAsset *)asset type:(ZLExportVideoType)type presetName:(NSString *)presetName watermarkImage:(UIImage *)watermarkImage watermarkLocation:(ZLWatermarkLocation)location imageSize:(CGSize)imageSize complete:(void (^)(NSString *exportFilePath, NSError *error))complete;
  163. /**
  164. 给视频加粒子特效,目前仅支持粒子从屏幕上方向下发射,e.g.:下雪特效,需传入一张雪花图片。
  165. @param asset 需要导出视频的asset
  166. @param type 视频导出格式
  167. @param presetName 视频压缩设置
  168. @param effectImage 粒子图片(建议一倍图尺寸不超过200*200)
  169. @param birthRate 粒子每秒创建数量(建议30~50)
  170. @param velocity 粒子扩散速度
  171. */
  172. + (void)exportVideoForAsset:(PHAsset *)asset type:(ZLExportVideoType)type presetName:(NSString *)presetName effectImage:(UIImage *)effectImage birthRate:(NSInteger)birthRate velocity:(CGFloat)velocity complete:(void (^)(NSString *exportFilePath, NSError *error))complete;
  173. /**
  174. 获取保存视频的路径
  175. */
  176. + (NSString *)getVideoExportFilePath:(ZLExportVideoType)type;
  177. #pragma mark - 相册、相机、麦克风权限相关
  178. /**
  179. 是否有相册访问权限
  180. */
  181. + (BOOL)havePhotoLibraryAuthority;
  182. /**
  183. 是否有相机访问权限
  184. */
  185. + (BOOL)haveCameraAuthority;
  186. /**
  187. 是否有麦克风访问权限
  188. */
  189. + (BOOL)haveMicrophoneAuthority;
  190. @end