图片放缩原理采用在UIScrollView上面添加UIImageView。UIScrollView的具体用法可参见:UIScrollView的用法。这篇文章写的很详细。
图片放缩的重点
1.控制图片放缩后展示的frame
2.为了完整的显示图片需要计算图片的大小。
3.加载网络图片
1.计算frame
/// 按照比例更改 ContentSize
func changegContentSize(zoomScale:CGFloat){
self.zoomScale = zoomScale
let horizontalPadding = self.imageView.frame.width < self.frame.width ? (self.frame.size.width - self.imageView.frame.size.width)/2.0 : 0.0
let verticalPadding = self.imageView.frame.height < self.frame.height ? (self.frame.size.height - self.imageView.frame.size.height)/2.0 : 0.0
let imageScaleWidth = zoomScale * self.normalWidth
let imageScaleHeight = zoomScale * self.normalHeight
var padding:CGFloat = 0.0
if imageScaleWidth < self.frame.width{
padding = (self.frame.width - imageScaleWidth)/2.0
let paddingY = (self.frame.height - imageScaleHeight)/2.0
if imageScaleHeight < self.frame.height{
self.imageView.frame = CGRect.init(x: padding, y: paddingY, width: imageScaleWidth, height: imageScaleHeight)
}else{
self.imageView.frame = CGRect.init(x: padding, y: verticalPadding, width: imageScaleWidth, height: imageScaleHeight)
}
}else{
self.imageView.frame = CGRect.init(x: horizontalPadding , y: verticalPadding, width: imageScaleWidth, height: imageScaleHeight)
}
}
2.计算图片大小
extension UIImage{
/// 获取
///
/// - Parameter view: 展示的view
/// - Returns: 图片大小
func getImageSize(view:UIView) -> CGSize {
var size = CGSize.zero
let sw = view.bounds.size.width
let sh = view.bounds.size.height
if self.size.width <= sw && self.size.height <= sh{
return self.size
}else if self.size.width > sw{
size.width = sw
size.height = (self.size.height/self.size.width) * sw
}else if self.size.height > sh{
size.height = sh
size.width = (self.size.width/self.size.height) * sh
}
return size
}
}
3.加载网络图片我采用的是
let data = NSData.init(contentsOf: URL.init(string: url!)!)
优点:方便快捷
缺点:当数据过多或者图片过大时加载很慢
优化方案1
利用多线程加载
self.createImagView(image: UIImage.init(named: "datadpload_photos") ?? UIImage())
DispatchQueue.global().async {
let data = NSData.init(contentsOf: URL.init(string: self.url!)!)
if data == nil{
DispatchQueue.main.async {
self.createImagView(image: UIImage.init(named: "datadpload_photos") ?? UIImage())
self.setZoomScale()
}
return
}
let image = UIImage.init(data: data! as Data)
if image == nil{
DispatchQueue.main.async {
self.createImagView(image: UIImage.init(named: "datadpload_photos") ?? UIImage())
self.setZoomScale()
}
return
}
DispatchQueue.main.async {
self.createImagView(image: image!)
self.setZoomScale()
}
}
优化方案2
利用SDWebImage
self.imageView?.sd_setImage(with: URL.init(string: self.url!)!, placeholderImage: UIImage.init(named: "datadpload_photos"), options: [], completed: { (image1, error, cacheMemoryOnly, url) in
if error == nil{
let size = image1?.getImageSize(view:self) ?? CGSize.zero
self.imageView?.bounds = CGRect.init(x: 0, y: 0, width: size.width, height: size.height)
self.imageView?.center = CGPoint.init(x: self.bounds.size.width/2 , y: self.bounds.size.height/2)
self.imageView?.image = image1
self.contentSize = self.imageView?.bounds.size ?? CGSize.zero
self.normalWidth = self.contentSize.width
self.normalHeight = self.contentSize.height
}
})
demo下载地址:demo
网友评论