Files
yuyin_ios/SweetParty/主类/Others/NewTools/MSCommonTool.m

113 lines
4.9 KiB
Mathematica
Raw Normal View History

2025-08-08 11:05:33 +08:00
//
// MSCommonTool.m
// misheng
//
// Created by bj_szd on 2022/10/19.
// Copyright © 2022 syllable interactive. All rights reserved.
//
#import "MSCommonTool.h"
@implementation MSCommonTool
+ (BRPickerStyle *)customPickerViewUI {
BRPickerStyle *style = [[BRPickerStyle alloc] init];
style.selectedColor = [UIColor whiteColor];
style.topCornerRadius = 10;
style.hiddenShadowLine = YES;
style.hiddenTitleLine = YES;
style.hiddenTitleLabel = YES;
style.titleBarColor = HEXCOLOR(0xF5F7F7);
style.cancelTextColor = HEXCOLOR(0x999999);
style.cancelTextFont = [UIFont boldSystemFontOfSize:16];
style.cancelBtnFrame = CGRectMake(0, 0, 60, 50);
style.cancelBtnTitle = @"取消";
style.doneTextColor = mainDeepColor;
style.doneTextFont = [UIFont boldSystemFontOfSize:16];
style.doneBtnFrame = CGRectMake(SCREEN_WIDTH-60, 0, 60, 50);
style.doneBtnTitle = @"确定";
style.separatorColor = [UIColor clearColor];
style.pickerTextColor = HEXCOLOR(0x333333);
style.pickerTextFont = [UIFont systemFontOfSize:16];
style.pickerColor = HEXCOLOR(0xF5F7F7);;
style.rowHeight = 44;
return style;
}
+ (UIImage *)convertViewToImage:(UIView *)view{
CGSize size = view.bounds.size;
//NOYES
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (void)saveImage:(UIImage *)image assetCollectionName:(NSString *)collectionName {
// 1. App
PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
// 2.
if (authorizationStatus == PHAuthorizationStatusAuthorized) {
// 2.1 , (2)
[self saveImage:image toCollectionWithName:collectionName];
} else if (authorizationStatus == PHAuthorizationStatusNotDetermined) { // , ,
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
// ,
if (status == PHAuthorizationStatusAuthorized) {
[self saveImage:image toCollectionWithName:collectionName];
}
}];
} else {
[HelpPageDefine showMessage:@"请在设置界面, 授权访问相册"];
}
}
//
+ (void)saveImage:(UIImage *)image toCollectionWithName:(NSString *)collectionName {
// 1.
PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
// 2. changeBlock
[library performChanges:^{
// 2.1
PHAssetCollectionChangeRequest *collectionRequest;
// 2.2
PHAssetCollection *assetCollection = [self getCurrentPhotoCollectionWithTitle:collectionName];
// 2.3
if (assetCollection) { // 使
collectionRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
} else { // ,
collectionRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:collectionName];
}
// 2.4 ,
PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// 2.4
PHObjectPlaceholder *placeholder = [assetRequest placeholderForCreatedAsset];
// 2.5
[collectionRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
// 3. , ,
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[HelpPageDefine showMessage:@"保存失败"];
} else {
[HelpPageDefine showMessage:@"保存成功"];
}
});
}];
}
+ (PHAssetCollection *)getCurrentPhotoCollectionWithTitle:(NSString *)collectionName {
// 1.
PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
// 2.
for (PHAssetCollection *assetCollection in result) {
if ([assetCollection.localizedTitle containsString:collectionName]) {
return assetCollection;
}
}
return nil;
}
@end