Skip to content

Commit

Permalink
refactor(all): 适配 iOS 17.0
Browse files Browse the repository at this point in the history
将UIGraphicsBeginImageContextWithOptions替换为UIGraphicsImageRenderer
  • Loading branch information
internetWei committed Nov 25, 2023
1 parent a6686d3 commit a7b1785
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 27 deletions.
10 changes: 4 additions & 6 deletions LLDynamicLaunchScreen/LLDynamicLaunchScreen+LLPrivate.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ - (nullable UIImage *)ll_imageByCropToRect:(CGRect)rect;
/// 获取图片指定位置上的颜色。
- (nullable UIColor *)ll_colorAtPoint:(CGPoint)point;


+ (nullable UIImage *)ll_snapshotImageForAView:(UIView *)aView;

@end


Expand Down Expand Up @@ -365,12 +368,7 @@ + (LLLaunchImageType)ll_getImageTypeFromPath:(NSString *)path imageName:(NSStrin

/// 对UIView指定区域进行截图。
+ (nullable UIImage *)ll_snapshotImageWithView:(UIView *)aView inRect:(CGRect)rect {
UIGraphicsBeginImageContextWithOptions(aView.bounds.size, aView.isOpaque, UIScreen.mainScreen.scale);
[aView drawViewHierarchyInRect:aView.bounds afterScreenUpdates:YES];
UIImage *wholeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return [wholeImage ll_imageByCropToRect:rect];
return [[UIImage ll_snapshotImageForAView:aView] ll_imageByCropToRect:rect];
}


Expand Down
14 changes: 8 additions & 6 deletions LLDynamicLaunchScreen/LLDynamicLaunchScreen.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ + (nullable id)ll_getAPPInfoForKey:(NSString *)aKey;
@end


@interface UIImage (LLPrivate)

+ (nullable UIImage *)ll_snapshotImageForAView:(UIView *)aView;

@end


@implementation LLDynamicLaunchScreen

+ (void)load {
Expand Down Expand Up @@ -132,12 +139,7 @@ + (nullable UIImage *)getSystemLaunchImageWithType:(LLLaunchImageType)type {
} break;
}

UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, UIScreen.mainScreen.scale);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *launchImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return launchImage;
return [UIImage ll_snapshotImageForAView:view];
};

UIImage * (^saveImage)(UIImage *) = ^ UIImage * (UIImage *image) {
Expand Down
76 changes: 62 additions & 14 deletions LLDynamicLaunchScreen/UIImage+LLPrivate.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,52 @@ - (nullable UIImage *)ll_imageByResizeToSize:(CGSize)size
contentMode:(UIViewContentMode)contentMode {
if (size.width <= 0 || size.height <= 0) return nil;

UIGraphicsBeginImageContextWithOptions(size, self.ll_isOpaque, self.scale);
[self ll_drawInRect:(CGRect){.size = size} contentMode:contentMode];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
if (@available(iOS 17.0, *)) {
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = self.scale;
format.opaque = self.ll_isOpaque;
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size format:format];

return [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
[self ll_drawInRect:(CGRect){.size = size} contentMode:contentMode];
}];
} else {
UIGraphicsBeginImageContextWithOptions(size, self.ll_isOpaque, self.scale);
[self ll_drawInRect:(CGRect){.size = size} contentMode:contentMode];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}


- (nullable UIImage *)ll_drawInRects:(NSArray<NSValue *> *)rects toColor:(UIColor *)color {
UIGraphicsBeginImageContextWithOptions(self.size, self.ll_isOpaque, self.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawInRect:(CGRect){.size = self.size}];
CGContextSetFillColorWithColor(context, color.CGColor);
for (NSValue *value in rects) {
CGContextFillRect(context, value.CGRectValue);
if (@available(iOS 17.0, *)) {
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = self.scale;
format.opaque = self.ll_isOpaque;
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:self.size format:format];

return [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawInRect:(CGRect){.size = self.size}];
CGContextSetFillColorWithColor(context, color.CGColor);
for (NSValue *value in rects) {
CGContextFillRect(context, value.CGRectValue);
}
}];
} else {
UIGraphicsBeginImageContextWithOptions(self.size, self.ll_isOpaque, self.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawInRect:(CGRect){.size = self.size}];
CGContextSetFillColorWithColor(context, color.CGColor);
for (NSValue *value in rects) {
CGContextFillRect(context, value.CGRectValue);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}


Expand Down Expand Up @@ -387,4 +414,25 @@ - (CGRect)ll_CGRectFitWithContentMode:(UIViewContentMode)mode size:(CGSize)size
return rect;
}


+ (nullable UIImage *)ll_snapshotImageForAView:(UIView *)aView {
UIImage *snapshotImage;

if (@available(iOS 17.0, *)) {
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.opaque = aView.isOpaque;
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:aView.bounds.size format:format];
snapshotImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
[aView drawViewHierarchyInRect:aView.bounds afterScreenUpdates:YES];
}];
} else {
UIGraphicsBeginImageContextWithOptions(aView.bounds.size, aView.isOpaque, UIScreen.mainScreen.scale);
[aView drawViewHierarchyInRect:aView.bounds afterScreenUpdates:YES];
snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}

return snapshotImage;
}

@end
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
LLDynamicLaunchScreen
==============
[![CI](https://github.com/internetWei/LLDynamicLaunchScreen/workflows/LLDynamicLaunchScreen%20CI/badge.svg)](https://github.com/internetWei/LLDynamicLaunchScreen/actions)&nbsp;&nbsp; [![Carthage](https://img.shields.io/badge/Carthage-compatible-brightgreen)](https://github.com/Carthage/Carthage)&nbsp; &nbsp;[![CocoaPods](https://img.shields.io/badge/pod-1.0.5-blue)](http://cocoapods.org/pods/LLDynamicLaunchScreen)&nbsp;&nbsp; [![Platform](https://img.shields.io/badge/platform-iOS-blue)](https://www.apple.com/nl/ios)&nbsp;&nbsp; [![License MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/internetWei/LLDynamicLaunchScreen/blob/master/LICENSE)
[![CI](https://github.com/internetWei/LLDynamicLaunchScreen/workflows/LLDynamicLaunchScreen%20CI/badge.svg)](https://github.com/internetWei/LLDynamicLaunchScreen/actions)&nbsp;&nbsp; [![Carthage](https://img.shields.io/badge/Carthage-compatible-brightgreen)](https://github.com/Carthage/Carthage)&nbsp; &nbsp;[![CocoaPods](https://img.shields.io/badge/pod-1.0.6-blue)](http://cocoapods.org/pods/LLDynamicLaunchScreen)&nbsp;&nbsp; [![Platform](https://img.shields.io/badge/platform-iOS-blue)](https://www.apple.com/nl/ios)&nbsp;&nbsp; [![License MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/internetWei/LLDynamicLaunchScreen/blob/master/LICENSE)

__LLDynamicLaunchScreen__ 是1个可以让你不用更新APP并修改iPhone上的各种启动图;它还可以自动修复启动图的各种显示异常。

Expand Down

0 comments on commit a7b1785

Please sign in to comment.