//
|
// HDLLinPhoneUtils.m
|
// HDLLinPhoneSDK
|
//
|
// Created by 陈启扬 on 2021/8/5.
|
// Copyright © 2021 陈启扬. All rights reserved.
|
//
|
|
#import "HDLLinPhoneCommon.h"
|
#import <Photos/Photos.h>
|
@implementation HDLLinPhoneCommon
|
+(UIViewController *) topMostController {
|
UIViewController*topController ;
|
if ([UIApplication sharedApplication].delegate.window) {
|
topController= [UIApplication sharedApplication].delegate.window.rootViewController;
|
}else{
|
topController=[self appWindow].rootViewController;
|
}
|
while(topController.presentedViewController){
|
topController=topController.presentedViewController;
|
}
|
return topController;
|
}
|
|
+(UIWindow*)appWindow{
|
UIWindow *window;
|
if (@available(iOS 13.0, *)) {
|
window = [UIApplication sharedApplication].windows[0];
|
if (!window) {
|
window=[UIApplication sharedApplication].delegate.window;
|
// DT(@"13delegateWindow:%@",window);
|
}
|
// DT(@"window:%@",window);
|
} else {
|
window = [UIApplication sharedApplication].delegate.window;
|
// DT(@"delegateWindow:%@",window);
|
}
|
return window;
|
}
|
|
+(NSString *)temporarySaveImagePath{
|
NSString *str;
|
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
|
NSUserDomainMask, YES);
|
NSLog(@"path:%@",path);
|
NSString *pictureName = @"snapShot.jpg";
|
str=[NSString stringWithFormat:@"%@/%@",path[0],pictureName];
|
return str;
|
}
|
|
#pragma 保存图片到相册
|
+(void)saveImageToPhotosAlbum:(UIImage *)savedImage
|
{
|
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
|
if (status == PHAuthorizationStatusNotDetermined)
|
{
|
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
|
if(status == PHAuthorizationStatusAuthorized)
|
{
|
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), NULL);
|
}
|
}];
|
}
|
else
|
{
|
if (status == PHAuthorizationStatusAuthorized)
|
{
|
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), NULL);
|
}
|
}
|
}
|
|
// 保存到相册指定回调方法
|
+(void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
|
{
|
NSString *message = nil;
|
NSString *languageName = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];
|
NSString *saveToTheAlbumsStr,*operationFailedStr,*tipStr,*okStr;
|
// 简体中文
|
if ([languageName rangeOfString:@"zh-Hans"].location != NSNotFound) {
|
tipStr = @"提示";
|
saveToTheAlbumsStr = @"已保存至手机相册.";
|
operationFailedStr = @"操作失败";
|
okStr = @"确认";
|
|
}else{
|
tipStr = @"Prompt";
|
okStr = @"OK";
|
saveToTheAlbumsStr = @"Saved to the albums.";
|
operationFailedStr = @"Operation failed.";
|
|
}
|
if (!error) {
|
message = saveToTheAlbumsStr;
|
}
|
else
|
{
|
message = operationFailedStr;
|
}
|
|
[self showUIAlertView:message title:tipStr cancelTitle:okStr];
|
}
|
|
+(void)showUIAlertView:(NSString *)mes title:(NSString *)title cancelTitle:(NSString *)cancleTitle
|
{
|
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:mes preferredStyle:UIAlertControllerStyleAlert];
|
[alertController addAction:[UIAlertAction actionWithTitle:cancleTitle style:UIAlertActionStyleCancel handler:nil]];
|
[[self topMostController] presentViewController:alertController animated:YES completion:nil];
|
|
}
|
|
//+(UIImage *)captureImageFromView:(UIView *)view
|
//{
|
// CGRect screenRect = [view bounds];
|
// UIGraphicsBeginImageContext(screenRect.size);
|
// CGContextRef ctx = UIGraphicsGetCurrentContext();
|
// [view.layer renderInContext:ctx];
|
// UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
|
// UIGraphicsEndImageContext();
|
// return image;
|
//}
|
|
+(UIImage *)captureImageFromView:(UIView *)view{
|
|
// // currentView 当前的view 创建一个基于位图的图形上下文并指定大小为
|
// UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.frame.size.width, view.frame.size.height), NO, 0.0);
|
// // renderInContext呈现接受者及其子范围到指定的上下文
|
// [view.layer renderInContext:UIGraphicsGetCurrentContext()];
|
// // 返回一个基于当前图形上下文的图片
|
// UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
|
// // 移除栈顶的基于当前位图的图形上下文
|
// UIGraphicsEndImageContext();
|
// // 保存图片
|
// //UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
|
// return viewImage;
|
|
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
|
|
// Render our snapshot into the image context
|
|
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
|
|
// Grab the image from the context
|
|
UIImage *complexViewImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
// Finish using the context
|
|
UIGraphicsEndImageContext();
|
|
return complexViewImage;
|
|
|
}
|
@end
|