113 lines
4.9 KiB
Mathematica
113 lines
4.9 KiB
Mathematica
|
|
//
|
|||
|
|
// 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;
|
|||
|
|
//下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
|
|||
|
|
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
|