swift使用SDPhotoBriwser瀏覽圖片教程
場景:我們在使用app的時候,特別是顯示多張圖片的時候,都會點擊圖片并進行瀏覽,比如QQ、微信,好友發(fā)表的動態(tài),我們都會點擊進去查看原圖。現(xiàn)在很多app都支持圖片瀏覽功能,這樣更加方便用戶體驗,那么我們在項目開發(fā)過程中,怎么對圖片點擊進行預(yù)覽,下面介紹一下在swift項目開發(fā)中使用SDPhotoBrowser進行圖片瀏覽。
SDPhotoBrowser地址:https://github.com/gsdios/SDPhotoBrowser
下面直接代碼進行說明
//
// ShopStoreTableHeaderView.swift
// SmartMilk
//
// Created by lin jiang on 2017/7/11.
// 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
// 自適應(yīng)父視圖
// self.mainScrollView.autoresizingMask = UIViewAutoresizing.FlexibleHeight
// 其他屬性
self.mainScrollView?.isScrollEnabled = true // 上下滾動
self.mainScrollView?.scrollsToTop = true // 點擊狀態(tài)欄時,可以滾動回頂端
self.mainScrollView?.bounces = true // 在最頂端或最底端時,仍然可以滾動,且釋放后有動畫返回效果
mainScrollView?.isPagingEnabled = true // 分頁顯示效果
mainScrollView?.showsHorizontalScrollIndicator = false // 顯示水平滾動條
mainScrollView?.showsVerticalScrollIndicator = true // 顯示垂直滾動條
mainScrollView?.indicatorStyle = UIScrollViewIndicatorStyle.white // 滑動條的樣式
// 設(shè)置內(nèi)容大小
// 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
// 其他屬性設(shè)置
self.mainPageControl?.numberOfPages = 0 // 總頁數(shù)
self.mainPageControl?.currentPage = 0 // 當前頁數(shù),默認為0,即第一個,實際數(shù)量是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 = URL(string: model.storeSmallImg!)
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)
//主要是顯示區(qū)分顯示大點的圖和小點的圖
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 // 總頁數(shù)
self.mainPageControl?.currentPage = 0 // 當前頁數(shù),默認為0,即第一個,實際數(shù)量是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
}
// 設(shè)置偏移量
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
//顯示圖片的總數(shù)量
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
}
}
//設(shè)置要顯示圖片資源的地址
func photoBrowser(_ browser: SDPhotoBrowser!, highQualityImageURLFor index: Int) -> URL! {
let model = self.storeImages[index] as! StoreImageModel
let url = URL(string: model.storeBigImg!)
return url
}
}
運行結(jié)果:以上圖片資源的地址就不提供了,是公司的資源,百度隨便找?guī)讖垐D片就可以了


以上就是swift使用SDPhotoBriwser瀏覽圖片教程的詳細內(nèi)容,更多關(guān)于使用SDPhotoBriwser瀏覽圖片的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Swift 5.1 之類型轉(zhuǎn)換與模式匹配的教程詳解
這篇文章主要介紹了Swift 5.1 之類型轉(zhuǎn)換與模式匹配的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Swift中defer關(guān)鍵字推遲執(zhí)行示例詳解
這篇文章主要給大家介紹了關(guān)于Swift中defer關(guān)鍵字推遲執(zhí)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
Swift map和filter函數(shù)原型基礎(chǔ)示例
這篇文章主要為大家介紹了Swift map和filter函數(shù)原型基礎(chǔ)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07

