Skip to content

egzonpllana/InteractiveImageView

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

“InteractiveImageView”

CocoaPods Carthage Swift Package Manager

Welcome to Interactive Image View, a simple library that provides an easier way to interact with image view, like scroll, zoom and crop. In its core it support two image content mode, the one is always square and the second one is custom. For example you can use aspect ration like instagram does 2:3 or 9:16, or any custom value. Basically, it's a thin wrapper around the UIScrollView and UIImageView APIs that UIKit provides.

Features

  • Use at any place as UIView, no need to present or configure a viewcontroller.
  • Crop image at current position as user wants.
  • Switch between aspect ration, 2:3 or 1:1, same as Instagram.
  • Can be extended to support different aspect rations.
  • Scroll image view on x and y axis.
  • Double tap to zoom in or zoom out.
  • Pinch image view.

Setup

  1. Add a view, and set the class of the view to InteractiveImageView.
  2. In your view controller, import InteractiveImageView.
  3. Connect view outlet, configure it with interactiveImageView.configure(...)
  4. Add delegates interactiveImageView.delegate = self
  5. Listen to delegate observers: extension ViewController: InteractiveImageViewDelegate { ... }

Methods

Configure view

if let image = UIImage(named: "image.png") {
    interactiveImageView.configure(withNextContentMode: .heightFill,
                      withFocusOffset: .center,
                      withImage: image)
}

Crop and get image without delegate methods

let croppedImage = interactiveImageView.cropAndGetImage()

Get original image without any modification on it.

let originalImage = interactiveImageView.getOriginalImage()

Set only image in the ImageView.

interactiveImageView.setImage(UIImage)

Toggle image content mode

interactiveImageView.toggleImageContentMode()

User gestures

Double tap to zoom gesture

interactiveImageView.isDoubleTapToZoomAllowed = false

Scroll view

interactiveImageView.isScrollEnabled = false

Pinch to zoom gesture

interactiveImageView.isPinchAllowed = false

Delegate methods

protocol InteractiveImageViewDelegate: AnyObject {
    func didCropImage(image: UIImage, fromView: InteractiveImageView)
    func didScrollAt(offset: CGPoint, scale: CGFloat, fromView: InteractiveImageView)
    func didZoomAt(offset: CGPoint, scale: CGFloat, fromView: InteractiveImageView)
    func didFail(_ fail: IIVFailType)
}

Example Project

You can download and run example project InteractiveImageViewExample.

Preview

“InteractiveImageView”

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate InteractiveImageView into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'InteractiveImageView'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate InteractiveImageView into your Xcode project using Carthage, specify it in your Cartfile:

github "egzonpllana/InteractiveImageView"

Swift Package Manager through Manifest File

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding InteractiveImageView as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/egzonpllana/InteractiveImageView.git", .upToNextMajor(from: "1.0.0"))
]

Swift Package Manager through XCode

To add InteractiveImageView as a dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter the repository URL

https://github.com/egzonpllana/InteractiveImageView.git

Backstory

So, why was this made? While I was working on a project to provide an interactive image view based on given aspect ration, I could not find a suitable solution that offers all in one these features working in a single view without a need for a viewcontroller, so I build it.

Questions or feedback?

Feel free to open an issue, or find me @egzonpllana on LinkedIn.