怎么做刷会员的网站,天津微外卖网站建设,做网站公司什么条件,17网一起做网店普宁潮汕什么是防抖#xff0c;为什么要防抖#xff1f;
比如我们在文档在线编辑中修改文档内容#xff0c;总不能打一个字就发送一次更新请求吧#xff0c;用户疯狂点击一个按钮#xff0c;总不能一直触发按钮的逻辑吧。防抖被用于避免频繁触发的事件。
Swift实现防抖代码…什么是防抖为什么要防抖
比如我们在文档在线编辑中修改文档内容总不能打一个字就发送一次更新请求吧用户疯狂点击一个按钮总不能一直触发按钮的逻辑吧。防抖被用于避免频繁触发的事件。
Swift实现防抖代码
import Foundationclass Debouncer {var delay: TimeIntervalvar timer: Timer?var closure: (() - Void)?init(delay: TimeInterval) {self.delay delay}func debounce(closure: escaping () - Void) {self.closure closuretimer?.invalidate()timer Timer.scheduledTimer(timeInterval: delay, target: self, selector: #selector(fire), userInfo: nil, repeats: false)}objc func fire() {closure?()}
}let debouncer Debouncer(delay: 0.5)func action() {print(Debounced action executed)
}debouncer.debounce {action()
}debouncer.debounce {action()
}什么是图片预加载为什么要预加载为什么要拼接
图片要被渲染到屏幕上要经历解码的阶段图片一解码就会大很多几MB的解码之后可能占几十MB的内存在Swift中图片在UIImageView设置.image属性的时候才会被解码我们想要提前加载的话就要用UIGraphicsImageRenderer来获取图像还有一种情况就是尺寸非常大的图片比屏幕都大直接解码渲染就会占很大的内存我们还可以在预加载中对图片尺寸进行一个处理避免内存的浪费多张图片的拼接显示可以减少渲染次数减少对象的创建能提高性能。
下面是Swift代码
import UIKitclass ViewController: UIViewController {lazy var imageView: UIImageView {let imageView UIImageView(frame: self.view.frame)return imageView}()override func viewDidLoad() {super.viewDidLoad()let imageUrls [URL(string: https://images.pexels.com/photos/356830/pexels-photo-356830.jpeg?autocompresscstinysrgbw1260h750dpr2)!,URL(string: https://images.pexels.com/photos/356830/pexels-photo-356830.jpeg?autocompresscstinysrgbw1260h750dpr2)!]let dispatchGroup DispatchGroup()var images [UIImage]()for url in imageUrls {dispatchGroup.enter()URLSession.shared.dataTask(with: url) { data, _, error indefer { dispatchGroup.leave() }if let data data, let image UIImage(data: data) {images.append(image)}}.resume()}dispatchGroup.notify(queue: .main) {if let combinedImage self.combineImages(images: images) {self.imageView.image combinedImageself.view.addSubview(self.imageView)}}}func combineImages(images: [UIImage]) - UIImage? {let renderer UIGraphicsImageRenderer(size: self.view.frame.size)let combinedImage renderer.image { context invar currentX 0.0for image in images {image.draw(at: CGPoint(x: currentX, y: 0))currentX image.size.width}}return combinedImage}
}本文的内容就到这里啦喜欢博主的可以点点关注。 文章转载自: http://www.morning.cnqdn.cn.gov.cn.cnqdn.cn http://www.morning.qsy41.cn.gov.cn.qsy41.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.hdlhh.cn.gov.cn.hdlhh.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.mkbc.cn.gov.cn.mkbc.cn http://www.morning.wwsgl.com.gov.cn.wwsgl.com http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.ypfw.cn.gov.cn.ypfw.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn http://www.morning.gnyhc.cn.gov.cn.gnyhc.cn http://www.morning.lrzst.cn.gov.cn.lrzst.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.c7624.cn.gov.cn.c7624.cn http://www.morning.gfrtg.com.gov.cn.gfrtg.com http://www.morning.xhwty.cn.gov.cn.xhwty.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.hlxpz.cn.gov.cn.hlxpz.cn http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn http://www.morning.dpnhs.cn.gov.cn.dpnhs.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.rxkq.cn.gov.cn.rxkq.cn http://www.morning.prxqd.cn.gov.cn.prxqd.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.lzqtn.cn.gov.cn.lzqtn.cn http://www.morning.fewhope.com.gov.cn.fewhope.com http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.yccnj.cn.gov.cn.yccnj.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.lblsx.cn.gov.cn.lblsx.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn http://www.morning.reababy.com.gov.cn.reababy.com http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.lqznq.cn.gov.cn.lqznq.cn http://www.morning.wcrcy.cn.gov.cn.wcrcy.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.tzjqm.cn.gov.cn.tzjqm.cn http://www.morning.cpqwb.cn.gov.cn.cpqwb.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.hpcpp.cn.gov.cn.hpcpp.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.flchj.cn.gov.cn.flchj.cn http://www.morning.txfxy.cn.gov.cn.txfxy.cn http://www.morning.yqpzl.cn.gov.cn.yqpzl.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn