WEImageViewer
Description
Table of Contents
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Installation
- via CocoaPods WEImageViewer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'WEImageViewer'
Supported Versions
- iOS 10.0 or later
Usage
UIImageView
imageView.enableViewer(true, presentViewController: self.navigationController)
// Or
imageView.enableViewer(true) // Viewer will be presented by UIApplication.share.keyWindows.rootViewController
// Or
let imageBrowser = WEImageViewController()
imageBrowser.show(self.navigationController, senderView: imageView)
UITableViewController, UICollectionViewController
Step 1. Declare the viewer as property
let imageViewer = WEImageViewController()
Step 2. In table view delegate function func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
, setup image viewer controller for the cell's image view
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! DemoCollectionViewCell
// Configure the cell
cell.coverImageView.imageViewerController = imageViewer
cell.coverImageView.enableViewer(true)
return cell
}
Step 3. Adopt WEImageViewerDataSource, WEImageViewerDelegate
func numberOfImageInViewer() -> Int {
return objects.count
}
func imageViewAtIndex(_ imageViewer: WEImageViewController, index: Int) -> UIImageView? {
let indexPath = IndexPath(row: index, section: 0)
if let cell = tableView.cellForRow(at: indexPath) {
return cell.contentView.viewWithTag(101) as? UIImageView
}
return nil
}
func frameInWindowForItemAtIndex(_ imageViewer: WEImageViewController, index: Int) -> CGRect {
let indexPath = IndexPath(row: index, section: 0)
let rect = tableView.rectForRow(at: indexPath)
return tableView.convert(rect, to: nil)
}
// Scroll to the cell has an image will be showing on viewer to make sure the dismissal animation will work correctly when user close viewer
func imageViewer(_ imageViewer: WEImageViewController, willShowAt index: Int) {
//This is my sample code, you can do some more to make it better
let indexPath = IndexPath(row: index, section: 0)
if let visibleIndexes = tableView.indexPathsForVisibleRows, !visibleIndexes.contains(indexPath) {
tableView.scrollToRow(at: indexPath, at: .middle, animated: false)
}
}
Attributes
Attribute for drawing | Value | Description |
---|---|---|
imagesDataSource |
WEImageViewerDataSource | The data source provides the image viewer controller object with the information it needs to construct and modify a viewer. |
delegate |
WEImageViewerDelegate | The delegate must adopt the WEImageViewerDelegate protocol. Optional methods of the protocol allow the delegate to manage some actions in viewer controller. |
rootViewController |
UIViewController | The controller will present the image viewer controller. The viewer would be presneted by keyWindow if rootViewController is nil |
selectedIndex |
Int | Index of current showing image in the viewer |
Public interface
WEImageViewerDataSource
func numberOfImageInViewer() -> Int
func imageViewAtIndex(_ imageViewer: WEImageViewController, index: Int) -> UIImageView?
func frameInWindowForItemAtIndex(_ imageViewer: WEImageViewController, index: Int) -> CGRect
optional func imageURLAtIndex(_ imageViewer: WEImageViewController, index: Int) -> URL?
WEImageViewerDelegate
optional func imageViewer(_ imageViewer: WEImageViewController , willShowAt index: Int)
Contact
You can contact me at email adress [email protected]. If you found any issues on the project, please open a ticket. Pull requests are also welcome.
License
WEImageViewer
is developed by GiangVT (aka Werfe) and is released under the MIT license. See the LICENSE
file for more details.
In my Sample project, background image is downloaded from ilikewallpaper and Pinterest. Thanks.