@@ -14,9 +14,26 @@ void swizzleClass(Class class, SEL originalAction, SEL swizzledAction) {
1414 method_exchangeImplementations (class_getClassMethod (class, originalAction), class_getClassMethod (class, swizzledAction));
1515}
1616
17+ void swizzleUIImageMethod (SEL originalAction, SEL swizzledAction) {
18+ Class class = [UIImage class ];
19+ Method originalMethod = class_getInstanceMethod (class, originalAction);
20+ Method swizzledMethod = class_getInstanceMethod (class, swizzledAction);
21+
22+ if (originalMethod && swizzledMethod) {
23+ method_exchangeImplementations (originalMethod, swizzledMethod);
24+ } else {
25+ NSLog (@" [UIKit+hook] Warning: Could not swizzle UIImage methods (%@ and %@ )" ,
26+ NSStringFromSelector (originalAction),
27+ NSStringFromSelector (swizzledAction));
28+ }
29+ }
30+
1731void init_hookUIKitConstructor (void ) {
1832 swizzle (UIDevice.class , @selector (userInterfaceIdiom ), @selector (hook_userInterfaceIdiom ));
1933 swizzle (UIImageView.class , @selector (setImage: ), @selector (hook_setImage: ));
34+
35+ // Add this line to swizzle the _imageWithSize: method
36+ swizzleUIImageMethod (NSSelectorFromString (@" _imageWithSize:" ), @selector (hook_imageWithSize: ));
2037
2138 if (realUIIdiom == UIUserInterfaceIdiomTV) {
2239 if (UIDevice.currentDevice .userInterfaceIdiom == UIUserInterfaceIdiomPad) {
@@ -72,6 +89,39 @@ - (void)hook_setImage:(UIImage *)image {
7289
7390@end
7491
92+ // Implementation of UIImage hook for proper sizing across iOS versions
93+ @implementation UIImage (hook)
94+
95+ - (UIImage *)hook_imageWithSize : (CGSize)size {
96+ if (CGSizeEqualToSize (self.size , size)) {
97+ return self;
98+ }
99+
100+ UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat ];
101+ format.scale = self.scale ;
102+ UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc ] initWithSize: size format: format];
103+
104+ UIImage *newImage = [renderer imageWithActions: ^(UIGraphicsImageRendererContext * _Nonnull context) {
105+ // Calculate proper proportions
106+ CGFloat widthRatio = size.width / self.size .width ;
107+ CGFloat heightRatio = size.height / self.size .height ;
108+ CGFloat ratio = MIN (widthRatio, heightRatio);
109+
110+ CGFloat newWidth = self.size .width * ratio;
111+ CGFloat newHeight = self.size .height * ratio;
112+
113+ // Center the image
114+ CGFloat x = (size.width - newWidth) / 2 ;
115+ CGFloat y = (size.height - newHeight) / 2 ;
116+
117+ [self drawInRect: CGRectMake (x, y, newWidth, newHeight)];
118+ }];
119+
120+ return [newImage imageWithRenderingMode: self .renderingMode];
121+ }
122+
123+ @end
124+
75125// Patch: unimplemented get/set UIToolbar functions on tvOS
76126@implementation UINavigationController (hook)
77127
0 commit comments