Skip to content

Commit f05f55e

Browse files
committed
ios14YYAnimatedImageView图片显示问题
1 parent 43cc722 commit f05f55e

File tree

6 files changed

+82
-5
lines changed

6 files changed

+82
-5
lines changed

iOS_Tips/.DS_Store

0 Bytes
Binary file not shown.

iOS_Tips/DarkMode.xcodeproj/project.pbxproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
2C5F11A7257BCB7000904B70 /* YYAnimatedImageView+iOS14.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C5F11A6257BCB7000904B70 /* YYAnimatedImageView+iOS14.m */; };
1011
2CC813682553CE88009DAC9B /* 已浏览.md in Resources */ = {isa = PBXBuildFile; fileRef = 2CC813672553CE88009DAC9B /* 已浏览.md */; };
1112
2CEBE85D24A6E65600BA21F3 /* SLTimer.m in Sources */ = {isa = PBXBuildFile; fileRef = 2CEBE85B24A6E65600BA21F3 /* SLTimer.m */; };
1213
780F5303236D8462000D0EA8 /* SLGridView.m in Sources */ = {isa = PBXBuildFile; fileRef = 780F5302236D8462000D0EA8 /* SLGridView.m */; };
@@ -214,6 +215,8 @@
214215
/* End PBXContainerItemProxy section */
215216

216217
/* Begin PBXFileReference section */
218+
2C5F11A5257BCB7000904B70 /* YYAnimatedImageView+iOS14.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "YYAnimatedImageView+iOS14.h"; sourceTree = "<group>"; };
219+
2C5F11A6257BCB7000904B70 /* YYAnimatedImageView+iOS14.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "YYAnimatedImageView+iOS14.m"; sourceTree = "<group>"; };
217220
2CC813672553CE88009DAC9B /* 已浏览.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = "已浏览.md"; sourceTree = "<group>"; };
218221
2CC8137925540CB8009DAC9B /* HMLog.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HMLog.h; sourceTree = "<group>"; };
219222
2CEBE85B24A6E65600BA21F3 /* SLTimer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SLTimer.m; sourceTree = "<group>"; };
@@ -605,6 +608,15 @@
605608
path = View;
606609
sourceTree = "<group>";
607610
};
611+
2C5F115C257BC96A00904B70 /* YYAnimatedImageViewCategory */ = {
612+
isa = PBXGroup;
613+
children = (
614+
2C5F11A5257BCB7000904B70 /* YYAnimatedImageView+iOS14.h */,
615+
2C5F11A6257BCB7000904B70 /* YYAnimatedImageView+iOS14.m */,
616+
);
617+
path = YYAnimatedImageViewCategory;
618+
sourceTree = "<group>";
619+
};
608620
7811F76C24B2C70D000AA044 /* 二进制重排 */ = {
609621
isa = PBXGroup;
610622
children = (
@@ -630,6 +642,7 @@
630642
7835047E24586BA30071283E /* ThirdLibary */ = {
631643
isa = PBXGroup;
632644
children = (
645+
2C5F115C257BC96A00904B70 /* YYAnimatedImageViewCategory */,
633646
789AD1CC24D9024300CB0B4C /* Reachability */,
634647
7857FD4824729E8500D3D986 /* BSBacktraceLogger */,
635648
78D3095D239688EF00DC373A /* OpenGLUtils */,
@@ -1537,6 +1550,7 @@
15371550
78B10482232F57C30051579F /* AppDelegate.m in Sources */,
15381551
78E72D7C24810E2D00751373 /* SLDynamicItem.m in Sources */,
15391552
78777CE0238FEA48006FA671 /* SLOpenGLController.m in Sources */,
1553+
2C5F11A7257BCB7000904B70 /* YYAnimatedImageView+iOS14.m in Sources */,
15401554
78908DC324FFBE5D004164C2 /* SLScrollviewNesteVC.m in Sources */,
15411555
7851CB362331CC87002295B5 /* SLDarkModeViewController.m in Sources */,
15421556
78D30965239688EF00DC373A /* GLESMath.c in Sources */,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// YYAnimatedImageView+iOS14.h
3+
// DarkMode
4+
//
5+
// Created by 王先生 on 2020/12/5.
6+
// Copyright © 2020 https://github.com/wsl2ls ----- . All rights reserved.
7+
//
8+
9+
#import "YYAnimatedImageView.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
///主要解决 ios14YYAnimatedImageView图片显示问题 #573 https://github.com/ibireme/YYKit/issues/573
14+
@interface YYAnimatedImageView (iOS14)
15+
16+
@end
17+
18+
NS_ASSUME_NONNULL_END
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// YYAnimatedImageView+iOS14.m
3+
// DarkMode
4+
//
5+
// Created by 王先生 on 2020/12/5.
6+
// Copyright © 2020 https://github.com/wsl2ls ----- . All rights reserved.
7+
//
8+
9+
#import "YYAnimatedImageView+iOS14.h"
10+
#import <objc/runtime.h>
11+
12+
@implementation YYAnimatedImageView (iOS14)
13+
+(void)load {
14+
// 获取系统的方法
15+
Method displayLayerMethod = class_getInstanceMethod(self, @selector(displayLayer:));
16+
// 获取更新的方法
17+
Method displayLayerNewMethod = class_getInstanceMethod(self, @selector(displayLayerNew:));
18+
// 方法交换
19+
method_exchangeImplementations(displayLayerMethod, displayLayerNewMethod);
20+
}
21+
-(void)displayLayerNew:(CALayer *)layer {
22+
Ivar imageIvar = class_getInstanceVariable([self class], "_curFrame");
23+
UIImage *image = object_getIvar(self, imageIvar);
24+
if (image) {
25+
layer.contents = (__bridge id)image.CGImage;
26+
}
27+
else {
28+
if (@available(iOS 14.0, *)) {
29+
[super displayLayer:layer];
30+
}
31+
}
32+
}
33+
@end

iOS_Tips/DarkMode/WKWebView/WebNative/SLReusableManager.m

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ @interface SLReusableManager ()
2828
@property (nonatomic, strong) NSMutableArray <SLReusableCell *>*visibleCells;
2929
///记录最后一次的偏移量,用来判断滑动方向
3030
@property (nonatomic, assign) CGFloat lastContentOffsetY;
31-
///顶部即将展示的索引1
31+
///顶部即将展示的索引
3232
@property (nonatomic, assign) NSInteger willDisplayIndexTop;
3333
///底部即将展示的索引
3434
@property (nonatomic, assign) NSInteger willDisplayIndexBottom;
@@ -216,7 +216,7 @@ - (void)willDisplayCellWithDirection:(BOOL)top {
216216
}
217217
}
218218
}
219-
//即将消失的cell,在消失时放入缓冲池里 top:YES上/NO下
219+
//即将消失的cell,在消失时放入缓冲池里,并且重置视图cell的内容 top:YES上/NO下
220220
- (void)willDisappearCellWithDirection:(BOOL)top {
221221
if(top) {
222222
if (self.willDisplayIndexTop+1 >= self.frameArray.count) return;
@@ -225,7 +225,13 @@ - (void)willDisappearCellWithDirection:(BOOL)top {
225225
self.willDisplayIndexTop = self.willDisplayIndexTop+1;
226226
NSLog(@"上 第 %ld 个cell消失",self.willDisplayIndexTop);
227227
SLReusableCell *cell = self.visibleCells.firstObject;
228-
cell.layer.contents = nil;
228+
229+
//进入缓冲池后,要清空重置cell上的内容,防止下一个取出时显示之前的内容,我这里重置时用了自己的默认logo,你可以自己重绘默认时的cell内容
230+
for (UIView *subView in cell.subviews) {
231+
subView.layer.contents = (__bridge id)[UIImage imageNamed:@"wsl"].CGImage;
232+
}
233+
cell.layer.contents = (__bridge id)[UIImage imageNamed:@"wsl"].CGImage;
234+
229235
NSHashTable * hashTable= self.reusablePool[cell.cellID];
230236
[hashTable addObject:cell];
231237
[self.visibleCells removeObjectAtIndex:0];
@@ -237,7 +243,13 @@ - (void)willDisappearCellWithDirection:(BOOL)top {
237243
self.willDisplayIndexBottom = self.willDisplayIndexBottom-1;
238244
NSLog(@"下 第 %ld 个cell消失",self.willDisplayIndexBottom);
239245
SLReusableCell *cell = self.visibleCells.lastObject;
240-
cell.layer.contents = nil;
246+
247+
//进入缓冲池后,要清空重置cell上的内容,防止下一个取出时显示之前的内容,我这里重置时用了自己的默认logo,你可以自己重绘默认时的cell内容
248+
for (UIView *subView in cell.subviews) {
249+
subView.layer.contents = (__bridge id)[UIImage imageNamed:@"wsl"].CGImage;
250+
}
251+
cell.layer.contents = (__bridge id)[UIImage imageNamed:@"wsl"].CGImage;
252+
241253
NSHashTable * hashTable= self.reusablePool[cell.cellID];
242254
[hashTable addObject:cell];
243255
[self.visibleCells removeLastObject];

iOS_Tips/DarkMode/WKWebView/WebNative/SLWebNativeViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#import "SLWebNativeViewController.h"
1010
#import <WebKit/WebKit.h>
11-
#import <YYImage.h>
11+
#import "YYImage.h"
1212
#import <YYWebImage.h>
1313
#import "SLAvPlayer.h"
1414
#import <YYModel.h>

0 commit comments

Comments
 (0)