IOS 图片预览

介绍

图片浏览器, 实现了单张图片浏览, 支持双击放大缩小, 手势捏合
不存在将黑边(图片未铺满屏幕的区域, 不能放大查看)

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// ImgPreViewController.swift
//
// Created by syc on 2018/10/18.
// Copyright © 2018年 623971951. All rights reserved.
//

import UIKit
import Foundation
import AVFoundation

// https://github.com/Krisiacik/ImageViewer
class ImgPreViewController: UIViewController {

var image: UIImage?
var imageView: UIImageView!
var scrollView: UIScrollView!
var activityIndicatorView: UIActivityIndicatorView!

override func viewDidLoad() {
super.viewDidLoad()

scrollView = UIScrollView()
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
scrollView.decelerationRate = UIScrollViewDecelerationRateFast
scrollView.contentInset = UIEdgeInsets.zero
scrollView.contentOffset = CGPoint.zero
scrollView.minimumZoomScale = 1
scrollView.maximumZoomScale = 3

scrollView.delegate = self

scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.new, context: nil)


imageView = UIImageView(frame: self.view.bounds)
imageView.contentMode = .scaleAspectFit
imageView.image = image

self.view.addSubview(scrollView)
scrollView.addSubview(imageView)

activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .white)
activityIndicatorView.startAnimating()
activityIndicatorView.stopAnimating()
view.addSubview(activityIndicatorView)

let single = UITapGestureRecognizer(target: self, action: #selector(self.singleAction(sender:)))
let double = UITapGestureRecognizer(target: self, action: #selector(self.doubleAction(sender:)))
single.numberOfTapsRequired = 1
double.numberOfTapsRequired = 2
single.require(toFail: double)
scrollView.addGestureRecognizer(single)
scrollView.addGestureRecognizer(double)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

scrollView.frame = self.view.bounds
activityIndicatorView.center = view.boundsCenter

if let size = imageView.image?.size , size != CGSize.zero {

let aspectFitItemSize = getAspectFitSize(forContentOfSize: size, inBounds: self.scrollView.bounds.size)

scrollView.contentSize = aspectFitItemSize
imageView.bounds.size = aspectFitItemSize
imageView.center = scrollView.boundsCenter
}
}
// Reports the continuous progress of Swipe To Dismiss to the Gallery View Controller
override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {

guard keyPath == "contentOffset" else { return }

}
deinit {
self.scrollView.removeObserver(self, forKeyPath: "contentOffset")
}

@objc func pinAction(sender: UIPinchGestureRecognizer){
// 捏合手势
let pin = UIPinchGestureRecognizer(target: self, action: #selector(self.pinAction(sender:)))
self.imageView.isUserInteractionEnabled = true
self.imageView.addGestureRecognizer(pin)

let factor:CGFloat = sender.scale
sender.view?.transform = CGAffineTransform(scaleX: factor, y: factor)
}
@objc func doubleAction(sender: UITapGestureRecognizer){
let touchPoint = sender.location(ofTouch: 0, in: imageView)
let aspectFillScale = getAspectFillZoomScale(forBoundingSize: scrollView.bounds.size, contentSize: imageView.bounds.size)

if (scrollView.zoomScale == 1.0 || scrollView.zoomScale > aspectFillScale) {

let zoomRectangle = zoomRect(ForScrollView: scrollView, scale: aspectFillScale, center: touchPoint)

UIView.animate(withDuration: 0.5, animations: { [weak self] in

self?.scrollView.zoom(to: zoomRectangle, animated: false)
})
}
else {
UIView.animate(withDuration: 0.5, animations: { [weak self] in

self?.scrollView.setZoomScale(1.0, animated: false)
})
}
}
@objc func singleAction(sender: Any){

}

}
extension ImgPreViewController: UIScrollViewDelegate{
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
imageView.center = getContentCenter(forBoundingSize: scrollView.bounds.size, contentSize: scrollView.contentSize)
}
}

extension ImgPreViewController{
// https://github.com/Krisiacik/ImageViewer
func getAspectFillZoomScale(forBoundingSize boundingSize: CGSize, contentSize: CGSize) -> CGFloat {

let aspectFitSize = aspectFitContentSize(forBoundingSize: boundingSize, contentSize: contentSize)
return (floor(boundingSize.width) == floor(aspectFitSize.width)) ? (boundingSize.height / aspectFitSize.height): (boundingSize.width / aspectFitSize.width)
}
func getAspectFitSize(forContentOfSize contentSize: CGSize, inBounds bounds: CGSize) -> CGSize {
return AVMakeRect(aspectRatio: contentSize, insideRect: CGRect(origin: CGPoint.zero, size: bounds)).size
}
func getContentCenter(forBoundingSize boundingSize: CGSize, contentSize: CGSize) -> CGPoint {

let horizontalOffset = (boundingSize.width > contentSize.width) ? ((boundingSize.width - contentSize.width) * 0.5): 0.0
let verticalOffset = (boundingSize.height > contentSize.height) ? ((boundingSize.height - contentSize.height) * 0.5): 0.0

return CGPoint(x: contentSize.width * 0.5 + horizontalOffset, y: contentSize.height * 0.5 + verticalOffset)
}
}

鸣谢

本文标题:IOS 图片预览

文章作者:史彦超

发布时间:2018年09月06日 - 22:09

最后更新:2021年07月20日 - 16:07

原始链接:https://doingself.github.io/2018/09/06/IOS-%E5%9B%BE%E7%89%87%E9%A2%84%E8%A7%88/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Donate comment here