-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXWNetImageNode.swift
52 lines (43 loc) · 1.3 KB
/
XWNetImageNode.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// XWNetImageNode.swift
// MIKS
//
// Created by Civel Xu on 2019/11/27.
// Copyright © 2019 xuxiwen. All rights reserved.
//
import UIKit
import Kingfisher
public class XWNetImageNode: ASControlNode {
private lazy var imageView: AnimatedImageView = {
let _imageView = AnimatedImageView()
_imageView.contentMode = .scaleAspectFill
_imageView.clipsToBounds = true
return _imageView
}()
public func safeImageView(handle: @escaping (_ imageView: UIImageView) -> Void) {
safeMainQueue { [weak self] in
guard let `self` = self else { return }
handle(self.imageView)
}
}
lazy var imageNode: ASDisplayNode = {
let _imageNode = ASDisplayNode { [weak self] () -> UIView in
return (self?.imageView ?? UIView())
}
return _imageNode
}()
override init() {
super.init()
addSubnode(imageNode)
}
private func safeMainQueue(closure: @escaping () -> Void) {
if Thread.current.isMainThread {
closure()
} else {
DispatchQueue.main.async(execute: closure)
}
}
override public func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
return ASInsetLayoutSpec(insets: .zero, child: imageNode)
}
}