cdts/xdts-ios 3/TreeHole/CYHResetCode/CYH/QMUIKit/QMUIComponents/ImagePickerLibrary/QMUIImagePickerHelper.m

148 lines
7.3 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// QMUIImagePickerHelper.m
// qmui
//
// Created by QMUI Team on 15/5/9.
//
#import "QMUIImagePickerHelper.h"
#import "QMUICore.h"
#import "QMUIAssetsManager.h"
#import "QMUIAsset.h"
#import <Photos/PHCollection.h>
#import <Photos/PHFetchResult.h>
#import "UIImage+QMUI.h"
#import "QMUILog.h"
static NSString * const kLastAlbumKeyPrefix = @"QMUILastestAlbumKeyWith";
static NSString * const kContentTypeOfLastAlbumKeyPrefix = @"QMUIContentTypeOfLastestAlbumKeyWith";
@implementation QMUIImagePickerHelper
+ (void)springAnimationOfImageSelectedCountChangeWithCountLabel:(UILabel *)label {
[self actionSpringAnimationForView:label];
}
+ (void)springAnimationOfImageCheckedWithCheckboxButton:(UIButton *)button {
[self actionSpringAnimationForView:button];
}
+ (void)actionSpringAnimationForView:(UIView *)view {
NSTimeInterval duration = 0.6;
CAKeyframeAnimation *springAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
springAnimation.values = @[@.85, @1.15, @.9, @1.0,];
springAnimation.keyTimes = @[@(0.0 / duration), @(0.15 / duration) , @(0.3 / duration), @(0.45 / duration),];
springAnimation.duration = duration;
[view.layer addAnimation:springAnimation forKey:@"imagePickerActionSpring"];
}
+ (void)removeSpringAnimationOfImageCheckedWithCheckboxButton:(UIButton *)button {
[button.layer removeAnimationForKey:@"imagePickerActionSpring"];
}
+ (QMUIAssetsGroup *)assetsGroupOfLastPickerAlbumWithUserIdentify:(NSString *)userIdentify {
// NSUserDefaults updateLastestAlbumWithAssetsGroup
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// 使 key updateLastestAlbumWithAssetsGroup QMUIAlbumContentType
NSString *lastAlbumKey = [NSString stringWithFormat:@"%@%@", kLastAlbumKeyPrefix, userIdentify];
NSString *contentTypeOflastAlbumKey = [NSString stringWithFormat:@"%@%@", kContentTypeOfLastAlbumKeyPrefix, userIdentify];
__block QMUIAssetsGroup *assetsGroup;
QMUIAlbumContentType albumContentType = (QMUIAlbumContentType)[userDefaults integerForKey:contentTypeOflastAlbumKey];
NSString *groupIdentifier = [userDefaults valueForKey:lastAlbumKey];
/**
* PHAssetCollection localIdentifier URL
* QMUI 2.0.0 QMUI AssetsLibrary 使
* groupIdentifier NSURL NSString
* NSString
*/
if (groupIdentifier && [groupIdentifier isKindOfClass:[NSString class]]) {
PHFetchResult *phFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[groupIdentifier] options:nil];
if (phFetchResult.count > 0) {
// PHFetchOptions
PHFetchOptions *phFetchOptions;
// albumContentType crash
if (albumContentType) {
phFetchOptions = [PHPhotoLibrary createFetchOptionsWithAlbumContentType:albumContentType];
}
PHAssetCollection *phAssetCollection = [phFetchResult firstObject];
assetsGroup = [[QMUIAssetsGroup alloc] initWithPHCollection:phAssetCollection fetchAssetsOptions:phFetchOptions];
}
} else {
QMUILog(@"QMUIImagePickerLibrary", @"Group For localIdentifier is not found! groupIdentifier is %@", groupIdentifier);
}
return assetsGroup;
}
+ (void)updateLastestAlbumWithAssetsGroup:(QMUIAssetsGroup *)assetsGroup ablumContentType:(QMUIAlbumContentType)albumContentType userIdentify:(NSString *)userIdentify {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// 使 key QMUIAssetsGroup QMUIAlbumContentType
NSString *lastAlbumKey = [NSString stringWithFormat:@"%@%@", kLastAlbumKeyPrefix, userIdentify];
NSString *contentTypeOflastAlbumKey = [NSString stringWithFormat:@"%@%@", kContentTypeOfLastAlbumKeyPrefix, userIdentify];
[userDefaults setValue:assetsGroup.phAssetCollection.localIdentifier forKey:lastAlbumKey];
[userDefaults setInteger:albumContentType forKey:contentTypeOflastAlbumKey];
[userDefaults synchronize];
}
+ (BOOL)imageAssetsDownloaded:(NSMutableArray<QMUIAsset *> *)imagesAssetArray {
for (QMUIAsset *asset in imagesAssetArray) {
if (asset.downloadStatus != QMUIAssetDownloadStatusSucceed) {
return NO;
}
}
return YES;
}
+ (void)requestImageAssetIfNeeded:(QMUIAsset *)asset completion: (void (^)(QMUIAssetDownloadStatus downloadStatus, NSError *error))completion {
if (asset.downloadStatus != QMUIAssetDownloadStatusSucceed) {
//
if (completion) {
completion(QMUIAssetDownloadStatusDownloading, nil);
}
[asset requestOriginImageWithCompletion:^(UIImage *result, NSDictionary<NSString *,id> *info) {
BOOL downloadSucceed = (result && !info) || (![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue]);
if (downloadSucceed) {
//
[asset updateDownloadStatusWithDownloadResult:YES];
if (completion) {
completion(QMUIAssetDownloadStatusSucceed, nil);
}
} else if ([info objectForKey:PHImageErrorKey]) {
//
[asset updateDownloadStatusWithDownloadResult:NO];
if (completion) {
completion(QMUIAssetDownloadStatusFailed, [info objectForKey:PHImageErrorKey]);
}
}
} withProgressHandler:^(double progress, NSError * _Nullable error, BOOL * _Nonnull stop, NSDictionary * _Nullable info) {
QMUILog(@"QMUIImagePickerLibrary", @"current progress is %f", progress);
asset.downloadProgress = progress;
}];
} else {
//
if (completion) {
completion(QMUIAssetDownloadStatusSucceed, nil);
}
}
}
@end