123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- //
- // JXHPhotoManager.m
- // 我的社保
- //
- // Created by shanp on 2020/9/15.
- //
- #import "JXHPhotoManager.h"
- #import "JXHPhotoAlbumManager.h"
- #import "JXHMacro.h"
- #import <JXHSystemShortcut.h>
- #import "SPSBGeneralManager.h"
- #import <NSObject+JXHNotification.h>
- @interface JXHPhotoManager () {
- UIImage *_originImage;
- UIImage *_thumbnailImage;
- CGSize _thumbnailImageSize;
- UIImage *_previewImage;
- CGSize _previewImageSize;
-
- PHImageRequestID _currentRequestId;
- }
- @end
- @implementation JXHPhotoManager
- - (instancetype)init {
- self = [super init];
- if (!self) return nil;
- @weakify(self)
- [self addNotificationName:UIApplicationDidReceiveMemoryWarningNotification block:^(id _Nullable object, NSDictionary * _Nullable userInfo) {
- @strongify(self)
- [self cleanCache];
- }];
- return self;
- }
- - (void)dealloc {
- [self removeNotificationName:UIApplicationDidReceiveMemoryWarningNotification];
- }
- - (BOOL)isEqual:(id)object {
- return [[object class] isEqual:[JXHPhotoManager class]] && [((JXHPhotoManager *)object).jxh_phAsset isEqual:self.jxh_phAsset];
- }
- #pragma mark - 原图
- - (nullable UIImage *)originImage {
- if (_originImage) {
- return _originImage;
- }
- __block UIImage *resultImage;
- PHImageRequestOptions *phImageRequestOptions = PHImageRequestOptions.new;
- phImageRequestOptions.synchronous = true;
- [[PHImageManager defaultManager] requestImageForAsset:_jxh_phAsset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:phImageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
- resultImage = result;
- }];
- _originImage = resultImage;
- return _originImage;
- }
- - (void)requestOriginImageWithCompletion:(void (^_Nullable)(UIImage * _Nullable, NSDictionary *_Nullable))completion withProgressHandler:(nullable PHAssetImageProgressHandler)phProgressHandler {
- if (_originImage) {
- // 如果已经有缓存的图片则直接拿缓存的图片
- if (completion) {
- completion(_originImage, nil);
- }
- return;
- }
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- [[PHImageManager defaultManager] cancelImageRequest:self->_currentRequestId];
- PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
- imageRequestOptions.networkAccessAllowed = true; // 允许访问网络
- imageRequestOptions.progressHandler = phProgressHandler;
- @weakify(self)
- self->_currentRequestId = [[PHImageManager defaultManager] requestImageForAsset:self->_jxh_phAsset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
- // 排除取消,错误,低清图三种情况,即已经获取到了高清图时,把这张高清图缓存到 _originImage 中
- @strongify(self)
- BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue];
- if (self && downloadFinined) {
- self->_originImage = result;
- }
- if (completion && downloadFinined) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completion(result, info);
- });
- }
-
- }];
- });
- }
- #pragma mark - 缩略图
- - (nullable UIImage *)thumbnailWithSize:(CGSize)size contentMode:(JXHImageContentMode)imageContentMode {
- if (_thumbnailImage && CGSizeEqualToSize(size, _thumbnailImageSize)) {
- return _thumbnailImage;
- }
- __block UIImage *resultImage;
- PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc] init];
- phImageRequestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
- // 在 PHImageManager 中,targetSize 等 size 都是使用 px 作为单位,因此需要对targetSize 中对传入的 Size 进行处理,宽高各自乘以 ScreenScale,从而得到正确的图片
- [[PHImageManager defaultManager] requestImageForAsset:_jxh_phAsset targetSize:CGSizeMake(size.width * jxh_screenScale(), size.height * jxh_screenScale()) contentMode:(PHImageContentMode)imageContentMode options:phImageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
- resultImage = result;
- }];
- _thumbnailImage = resultImage;
- _thumbnailImageSize = size;
- return _thumbnailImage;
- }
- - (void)requestThumbnailImageWithSize:(CGSize)size contentMode:(JXHImageContentMode)imageContentMode completion:(void (^_Nullable)(UIImage *_Nullable, NSDictionary *_Nullable))completion {
- if (_thumbnailImage && CGSizeEqualToSize(size, _thumbnailImageSize)) {
- if (completion) {
- completion(_thumbnailImage, nil);
- }
- return;
- }
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- [[PHImageManager defaultManager] cancelImageRequest:self->_currentRequestId];
- PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
- imageRequestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
- // 在 PHImageManager 中,targetSize 等 size 都是使用 px 作为单位,因此需要对targetSize 中对传入的 Size 进行处理,宽高各自乘以 ScreenScale,从而得到正确的图片
- @weakify(self)
- self->_currentRequestId = [[PHImageManager defaultManager] requestImageForAsset:self->_jxh_phAsset targetSize:CGSizeMake(size.width * jxh_screenScale(), size.height * jxh_screenScale()) contentMode:(PHImageContentMode)imageContentMode options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
- @strongify(self)
- // 排除取消,错误,低清图三种情况,即已经获取到了高清图时,把这张高清图缓存到 _thumbnailImage 中
- // debugLog(@"%@", [NSThread currentThread]);
- BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue];
- if (self && downloadFinined) {
- self->_thumbnailImage = result;
- self->_thumbnailImageSize = size;
- }
- if (completion && downloadFinined) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completion(result, info);
- });
- }
-
- }];
- });
- }
- #pragma mark - 预览图
- - (nullable UIImage *)previewImageWithSize:(CGSize)size {
- if (_previewImage && CGSizeEqualToSize(size, _previewImageSize)) {
- return _previewImage;
- }
- __block UIImage *resultImage;
- PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
- imageRequestOptions.synchronous = true;
- [[PHImageManager defaultManager] requestImageForAsset:_jxh_phAsset targetSize:CGSizeMake(size.width * jxh_screenScale(), size.height * jxh_screenScale()) contentMode:PHImageContentModeAspectFit options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
- resultImage = result;
- }];
- _previewImage = resultImage;
- _previewImageSize = size;
- return _previewImage;
- }
- - (void)requestPreviewImageWithSize:(CGSize)size completion:(void (^_Nullable)(UIImage *_Nullable, NSDictionary *_Nullable))completion progressHandler:(PHAssetImageProgressHandler)phProgressHandler {
- if (_previewImage && CGSizeEqualToSize(size, _previewImageSize)) {
- // 如果已经有缓存的图片则直接拿缓存的图片
- if (completion) {
- completion(_previewImage, nil);
- }
- return;
- }
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- [[PHImageManager defaultManager] cancelImageRequest:self->_currentRequestId];
- PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
- imageRequestOptions.networkAccessAllowed = true; // 允许访问网络
- imageRequestOptions.progressHandler = phProgressHandler;
- @weakify(self)
- self->_currentRequestId = [[PHImageManager defaultManager] requestImageForAsset:self->_jxh_phAsset targetSize:CGSizeMake(size.width * jxh_screenScale(), size.height * jxh_screenScale()) contentMode:PHImageContentModeAspectFit options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
- @strongify(self)
- // 排除取消,错误,低清图三种情况,即已经获取到了高清图时,把这张高清图缓存到 _previewImage 中
- BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue];
- if (self && downloadFinined) {
- self->_previewImage = result;
- self->_previewImageSize = size;
- }
-
- if (completion && downloadFinined) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completion(result, info);
- });
- }
- }];
- });
-
- }
- - (void)getCover {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- if (self->_jxh_phAsset.mediaType != PHAssetMediaTypeVideo) {
- return;
- }
- if (self->_jxh_playItem) {
- return;
- }
- [[PHImageManager defaultManager] cancelImageRequest:self->_currentRequestId];
- PHVideoRequestOptions *videoRequestOptions = PHVideoRequestOptions.new;
- videoRequestOptions.networkAccessAllowed = true;
- self->_currentRequestId = [[PHImageManager defaultManager] requestPlayerItemForVideo:self->_jxh_phAsset options:videoRequestOptions resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
- if (self && playerItem) {
- self->_jxh_playItem = playerItem;
- }
- }];
- });
- }
- - (void)requestVideoWithCompletion:(void (^_Nullable)(AVPlayerItem *_Nullable, NSDictionary *_Nullable))completion progressHandler:(nullable PHAssetVideoProgressHandler)progressHandler {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- [[PHImageManager defaultManager] cancelImageRequest:self->_currentRequestId];
- PHVideoRequestOptions *videoRequestOptions = PHVideoRequestOptions.new;
- videoRequestOptions.networkAccessAllowed = true;
- videoRequestOptions.progressHandler = progressHandler;
- self->_currentRequestId = [[PHImageManager defaultManager] requestPlayerItemForVideo:self->_jxh_phAsset options:videoRequestOptions resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
- if (self && playerItem) {
- self->_jxh_playItem = playerItem;
- }
- if (completion) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completion(playerItem, info);
- });
- }
- }];
- });
-
- }
- - (void)cleanCache {
- _originImage = nil;
- _thumbnailImage = nil;
- _previewImage = nil;
- }
- @end
|