Skip to content

Instantly share code, notes, and snippets.

View guzhenhuaGitHub's full-sized avatar

o( ̄へ ̄)o guzhenhuaGitHub

View GitHub Profile
@guzhenhuaGitHub
guzhenhuaGitHub / bitmapImage.swift
Created May 21, 2021 03:37
图像的绘制通常是指用那些以 CG 开头的方法把图像绘制到画布中,然后从画布创建图片并显示这样一个过程。这个最常见的地方就是 [UIView drawRect:] 里面了。由于 CoreGraphic 方法通常都是线程安全的,所以图像的绘制可以很容易的放到后台线程进行。
public extension CGImage {
func bitmapImage() -> CGImage? {
guard
let colorSpace = colorSpace,
let context = CGContext(
data: nil,
width: width,
height: height,
import UIKit
// According to article by John Sundell
// https://www.swiftbysundell.com/articles/caching-in-swift/
final class Cache<Key: Hashable, Value> {
private let wrapped = NSCache<WrappedKey, Entry>()
private let dateProvider: () -> Date
private let entryLifetime: TimeInterval
private let keyTracker = KeyTracker()
@guzhenhuaGitHub
guzhenhuaGitHub / PopGestureConflictProtocol.swift
Last active June 14, 2019 10:49
解决scrollView右滑与系统右滑返回上级页面手势冲突
/// 解决视图右滑与系统右滑返回上级页面手势冲突
protocol PopGestureConflictProtocol {
}
extension UIViewController: PopGestureConflictProtocol {}
extension PopGestureConflictProtocol where Self: UIViewController {
/// 处理scrollView和系统右滑返回手势的冲突
///
/// 虽然self可能是某个UIViewController的子视图,但是不用管它。直接处理navigationController的手势冲突就行了
public extension UIImage {
/// 圆角图片
func roundCornerImage(withCornerRadius cornerRadius: CGFloat) -> UIImage? {
// 开关图形上下文
UIGraphicsBeginImageContext(size)
defer { UIGraphicsEndImageContext() }
// 获取图形上下文
let context = UIGraphicsGetCurrentContext()
// 带圆角的长方形贝塞尔曲线
let roundedRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
@guzhenhuaGitHub
guzhenhuaGitHub / KeyPath.swift
Last active December 5, 2018 09:59
Sequence with KeyPath
extension Sequence {
func map<T>(_ keyPath: KeyPath<Element, T>) -> [T] {
return map { $0[keyPath: keyPath] }
}
}
extension Sequence {
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>) -> [Element] {
return sorted { a, b in
return a[keyPath: keyPath] < b[keyPath: keyPath]
@guzhenhuaGitHub
guzhenhuaGitHub / cow.sh
Created November 23, 2018 09:15
shell command, just for fun
alias cow=fortune | cowsay | lolcat
@guzhenhuaGitHub
guzhenhuaGitHub / PasswordRule.swift
Last active October 24, 2018 02:40
Swift密码规则封装
enum PasswordRule {
enum CharacterClass {
case upper, lower, digits, special, asciiPrintable, unicode
case custom(Set<Character>)
}
case required(CharacterClass)
case allowed(CharacterClass)
case maxConsecutive(UInt)
case minLength(UInt)
@guzhenhuaGitHub
guzhenhuaGitHub / Vue.filter+Lodash.js
Last active September 27, 2018 02:14
add lodash functions to Vue.filter
/* 将Lodash的方法注入到Vue的filter中 */
_.each(_, (v, k) => {
if (_.isFunction(v)) {
Vue.filter('_' + k, (input, args) => {
return v.apply(_, _.flatten([[input], args]))
})
}
})
@guzhenhuaGitHub
guzhenhuaGitHub / EachConsecutive.swift
Created September 21, 2018 03:47
helper like ruby's Enumerable#each_cons
extension Collection {
func eachConsecutive(_ size: Int) -> [[Element]] {
let droppedIndices = indices.dropFirst(size - 1)
return zip(indices, droppedIndices)
.map {
return Array(self[$0...$1])
}
}
}
@guzhenhuaGitHub
guzhenhuaGitHub / EachSlice.swift
Created September 21, 2018 03:46
helper like ruby's Enumerable#each_slice
extension Array {
func eachSlice(_ step: Int) -> [[Element]] {
return stride(from: 0, through: count, by: step)
.lazy
.map { from in
let to = Swift.min(from + step, count)
return (from..<to)
.lazy
.map { [self[$0]] }
.reduce([], +)