在项目中我们经常要实现图片预览这个功能,简单的做法就是往scrollview中添加子控件以实现目的,但是一旦要预览的图片很多的时候,这样的做法会创建很多多余的控件,性能上会大打折扣,下面为大家提供一个思路,来解决这种不开心的事情.
思路如下
** 前提我们只需要创建两个图片视图,因为显示的图片时候我们只会同时显示一张图片,在图片滑动的时候我们才会同时两张图片,照着这样的思路走下去,首先我们得创建两个数组,第一个数组用于存放已创建为使用的图形控件对象,第二个用于存放活动中(显示在屏幕中)的图形对象.当滑动控件进行滑动的时候,首先检查缓存数组中是否对象存在,有则取(取完之后需要移除)无则创建,并且都需要存放在活动中的数组中,当划过一页边界的时候应当从活动数组中移除消失在视野中图形控件并存放在缓存数组中,以此类推便可.**
下面是代码实现
let screenBounds = UIScreen.mainScreen().bounds
typealias CallBack = (a:Int,b:Int)->Int
//可循环利用的ScrollView
class LooperPhoto: UIScrollView,UIScrollViewDelegate{
// 缓存的图片控件
var cacheImages = NSMutableArray()
// 活动的图片控件
var actionImages = NSMutableArray()
// var photos:[JSON]?
private var dataSources:[AnyObject]?
var imageType = ImageType.Web
var pageCount = 0{
didSet{
if pageCount>oldValue{
self.contentSize = CGSizeMake(CGFloat(pageCount)*CGRectGetWidth(self.bounds), 0)
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
initSetting()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
initSetting()
}
private func initSetting(){
self.showsHorizontalScrollIndicator = false
self.showsVerticalScrollIndicator = false
self.pagingEnabled = true
self.delegate = self
}
func setCurrentPage(page:Int){
self.setContentOffset(CGPointMake(CGFloat(page)*screenBounds.width, 0), animated: false)
}
func setImageDataSource(dataSouce:[AnyObject],imageType:ImageType){
self.pageCount = dataSouce.count
self.imageType = imageType
self.dataSources = dataSouce
showImageAtIndex(0)
}
func showImageAtIndex(index:Int){
var imageView:UIImageView?
if (cacheImages.count == 0){
imageView = getImageView(index)
}else{
imageView = cacheImages.firstObject as? UIImageView
imageView?.frame.origin.x = CGFloat(index)*self.bounds.width
cacheImages.removeObjectAtIndex(0)
}
setImageToImageView(index,imageView: imageView!)
imageView!.tag = index
actionImages.addObject(imageView!)
self.addSubview(imageView!)
}
private func setImageToImageView(index:Int,imageView:UIImageView){
let data:AnyObject = dataSources![index]
var image:UIImage?
switch imageType{
case .Local:
let alAsset = data as! ALAsset
image = UIImage(CGImage: alAsset.defaultRepresentation().fullScreenImage().takeUnretainedValue())
case .Web:
let imageUrl = data as! String
imageView.sd_setImageWithURL(NSURL(string: imageUrl))
case .Resource:
let resourceName = data as! String
image = UIImage(named: resourceName)
}
if (nil != image){
imageView.image = image!
}
}
func getImageView(index:Int)->UIImageView{
let width = CGRectGetWidth(self.bounds)
let imageView = UIImageView()
var frame = self.bounds
frame.origin.x = CGFloat(index)*width
imageView.frame = frame
imageView.contentMode = UIViewContentMode.ScaleAspectFit
return imageView
}
//总共就2个image控件
func scrollViewDidScroll(scrollView: UIScrollView) {
let scrollViewBounds = scrollView.bounds
let minX = CGRectGetMinX(scrollViewBounds)
let maxX = CGRectGetMaxX(scrollViewBounds)
var firstIndex = Int(minX/scrollViewBounds.width)
var lastIndex = Int(maxX/scrollViewBounds.width)
if firstIndex<0{
firstIndex = 0
}
if lastIndex >= pageCount{
lastIndex = pageCount-1
}
var tempImagesArrary = [UIImageView]()
for imageView in actionImages{
let index = imageView.tag
if index<firstIndex || index>lastIndex{
imageView.removeFromSuperview()
tempImagesArrary.append(imageView as! UIImageView)
cacheImages.addObject(imageView)
}
}
for imageView in tempImagesArrary{
actionImages.removeObject(imageView)
}
//TOO-DO 这里要将不显示的imageView从ActionImages移除
for (var i = firstIndex;i<=lastIndex;i++){
var isShowNewImage = true
for imageview in actionImages{
let tagIndex = imageview.tag
if tagIndex == i{
isShowNewImage = false
}
}
if isShowNewImage{
showImageAtIndex(i)
}
}
}
enum ImageType{
case Local,Web,Resource
}
}
网友评论