场景:我们在使用app的时候,特别是显示多张图片的时候,都会点击图片并进行浏览,比如qq、微信,好友发表的动态,我们都会点击进去查看原图。现在很多app都支持图片浏览功能,这样更加方便用户体验,那么我们在项目开发过程中,怎么对图片点击进行预览,下面介绍一下在swift项目开发中使用sdphotobrowser进行图片浏览。
sdphotobrowser地址:
下面直接代码进行说明
// // shopstoretableheaderview.swift // smartmilk // // created by lin jiang on 2017/7/11. // d88尊龙官网手机app copyright © 2017年 greengao. all rights reserved. // import uikit //添加sdphotobrowserdelegate代理 class shopstoretableheaderview: uiview,uiscrollviewdelegate,sdphotobrowserdelegate { var storeimages:nsmutablearray = nsmutablearray() var mainscrollview:uiscrollview? var mainpagecontrol:uipagecontrol? var maintimer:timer? var ismiddleshow:bool = false override init(frame: cgrect) { super.init(frame: frame) setscrollviewui() setpagecontrolui() } required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } func setscrollviewui(){ self.mainscrollview = uiscrollview(frame: cgrect(x: 0, y: 0, width: self.width, height: self.height)) // 添加到父视图 self.addsubview(self.mainscrollview!) // 背景颜色 // arc4random() self.mainscrollview?.backgroundcolor = uicolor.white // 自适应父视图 // self.mainscrollview.autoresizingmask = uiviewautoresizing.flexibleheight // 其他属性 self.mainscrollview?.isscrollenabled = true // 上下滚动 self.mainscrollview?.scrollstotop = true // 点击状态栏时,可以滚动回顶端 self.mainscrollview?.bounces = true // 在最顶端或最底端时,仍然可以滚动,且释放后有动画返回效果 mainscrollview?.ispagingenabled = true // 分页显示效果 mainscrollview?.showshorizontalscrollindicator = false // 显示水平滚动条 mainscrollview?.showsverticalscrollindicator = true // 显示垂直滚动条 mainscrollview?.indicatorstyle = uiscrollviewindicatorstyle.white // 滑动条的样式 // 设置内容大小 // self.mainscrollview?.contentsize = cgsize(width: originx, height: scrollviewheight) // 代理 self.mainscrollview?.delegate = self } func setpagecontrolui(){ self.mainpagecontrol = uipagecontrol(frame: cgrect(x: (self.width - 150.0) / 2, y: ((self.mainscrollview?.bounds)!.height - 20.0 - 10.0), width: 150.0, height: 20.0)) // 添加到父视图 self.addsubview(self.mainpagecontrol!) // 背景颜色 self.mainpagecontrol?.backgroundcolor = uicolor.clear // 其他属性设置 self.mainpagecontrol?.numberofpages = 0 // 总页数 self.mainpagecontrol?.currentpage = 0 // 当前页数,默认为0,即第一个,实际数量是0~n-1 self.mainpagecontrol?.pageindicatortintcolor = uicolor.lightgray // 非当前页颜色 self.mainpagecontrol?.currentpageindicatortintcolor = uicolor.red // 当前页颜色 } func setmainscrollviewpagecontrol(images:[storeimagemodel]) { self.storeimages.removeallobjects() self.storeimages.addobjects(from: images) for view in (self.mainscrollview?.subviews)! { view.removefromsuperview() } var originx:cgfloat = 0.0 for model in images { let imageview = uiimageview(frame: cgrect(x:originx, y:0.0, width:screen_width, height:self.height)) let url = let image = uiimage(named: "default_icon") // weak var weakself = self imageview.sd_setimage(with: url, placeholderimage: image, options: .retryfailed, completed: { (image, error, cachetype, url) in }) //添加ui image view的点击事情 let tap = uitapgesturerecognizer(target: self, action: #selector(onclickedimageevent(gest:))) imageview.isuserinteractionenabled = true imageview.addgesturerecognizer(tap) //主要是显示区分显示大点的图和小点的图 if ismiddleshow == true{ let contentview = uiview(frame: cgrect(x:originx, y:0.0, width:self.width, height:self.height)) let imagexx = (screen_width - self.height)/2 imageview.frame = cgrect(x:imagexx, y:0.0, width:self.height, height:self.height) contentview.addsubview(imageview) self.mainscrollview?.addsubview(contentview) originx = (contentview.frame.minx contentview.frame.width) } else { self.mainscrollview?.addsubview(imageview) originx = (imageview.frame.minx imageview.frame.width) } } self.mainscrollview?.contentsize = cgsize(width: originx, height: self.height) self.mainpagecontrol?.numberofpages = images.count // 总页数 self.mainpagecontrol?.currentpage = 0 // 当前页数,默认为0,即第一个,实际数量是0~n-1 } func scrollviewdidenddecelerating(_ scrollview: uiscrollview) { mqlog(message:"6 scrollviewdidenddecelerating") let width = scrollview.frame.width let offsetx = scrollview.contentoffset.x let index = offsetx / width mqlog(message:"当前页是:\(index)") self.mainpagecontrol?.currentpage = int(index) } func addtimer() { self.maintimer = timer.scheduledtimer(timeinterval: 2.0, target: self, selector: #selector(autoshow), userinfo: nil, repeats: true) } func removertimer() { if self.maintimer != nil { self.maintimer?.invalidate() self.maintimer = nil } } func autoshow() { var page:int = (self.mainpagecontrol?.currentpage)! let total = (self.mainpagecontrol?.numberofpages)! - 1 if total == page { page = 0 } else { page = 1 } // 设置偏移量 let offsetx = cgfloat(page) * (self.mainscrollview?.frame)!.width self.mainscrollview?.setcontentoffset(cgpoint(x:offsetx, y:0.0), animated: true) self.mainpagecontrol?.currentpage = page } func onclickedimageevent(gest:uitapgesturerecognizer) { let browser = sdphotobrowser() //显示图片uiimageview的父控件 browser.sourceimagescontainerview = self.mainscrollview //显示图片的总数量 browser.imagecount = self.storeimages.count if self.storeimages.count > (self.mainpagecontrol?.currentpage)! { browser.currentimageindex = (self.mainpagecontrol?.currentpage)! } else { //要显示的当前图片下标位置 browser.currentimageindex = 0 } browser.delegate = self browser.show() } /**************************************************************/ //返回当前uiimageview显示的图片 func photobrowser(_ browser: sdphotobrowser!, placeholderimagefor index: int) -> uiimage! { if self.ismiddleshow{ let contentview = self.mainscrollview?.subviews[index] let imageview:uiimageview = contentview?.subviews[0]as! uiimageview return imageview.image } else { let imageview:uiimageview = self.mainscrollview?.subviews[index]as! uiimageview return imageview.image } } //设置要显示图片资源的地址 func photobrowser(_ browser: sdphotobrowser!, highqualityimageurlfor index: int) -> url! { let model = self.storeimages[index] as! storeimagemodel let url = return url } }
运行结果:以上图片资源的地址就不提供了,是公司的资源,百度随便找几张图片就可以了
以上就是swift使用sdphotobriwser浏览图片教程的详细内容,更多关于使用sdphotobriwser浏览图片的资料请关注其它相关文章!