Skip to content

Commit

Permalink
Don't snapshot view, use a live blur. Fixes #152
Browse files Browse the repository at this point in the history
  • Loading branch information
JanGorman committed Jan 14, 2020
1 parent f9e3367 commit 990e7c3
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Agrume.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "Agrume"
s.version = "5.5.0"
s.version = "5.6.0"
s.summary = "An iOS image viewer written in Swift."
s.swift_version = "5.0"

Expand Down
17 changes: 5 additions & 12 deletions Agrume/Agrume.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ public final class Agrume: UIViewController {
fatalError("Not implemented")
}

private var backgroundSnapshot: UIImage!
private var backgroundImageView: UIImageView!
private var _blurContainerView: UIView?
private var blurContainerView: UIView {
if _blurContainerView == nil {
Expand Down Expand Up @@ -229,12 +227,10 @@ public final class Agrume: UIViewController {
///
/// - Parameters:
/// - viewController: The UIViewController to present from
/// - backgroundSnapshotVC: Optional UIViewController that will be used as basis for a blurred background
public func show(from viewController: UIViewController, backgroundSnapshotVC: UIViewController? = nil) {
backgroundSnapshot = (backgroundSnapshotVC ?? viewControllerForSnapshot(fromViewController: viewController))?.view.snapshot()
public func show(from viewController: UIViewController) {
view.isUserInteractionEnabled = false
addSubviews()
show(from: viewController)
present(from: viewController)
}

/// Update image at index
Expand Down Expand Up @@ -279,10 +275,7 @@ public final class Agrume: UIViewController {

private func addSubviews() {
view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
backgroundImageView = UIImageView(frame: view.frame)
backgroundImageView.image = backgroundSnapshot
view.addSubview(backgroundImageView)


if case .blurred = background {
blurContainerView.addSubview(blurView)
}
Expand All @@ -291,7 +284,7 @@ public final class Agrume: UIViewController {
view.addSubview(spinner)
}

private func show(from viewController: UIViewController) {
private func present(from viewController: UIViewController) {
DispatchQueue.main.async {
self.blurContainerView.alpha = 1
self.collectionView.alpha = 0
Expand Down Expand Up @@ -374,7 +367,7 @@ public final class Agrume: UIViewController {
layout.itemSize = view.bounds.size
layout.invalidateLayout()

backgroundImageView.frame = view.bounds
// backgroundImageView.frame = view.bounds
spinner.center = view.center
collectionView.frame = view.bounds

Expand Down
22 changes: 1 addition & 21 deletions Agrume/UIKit+Agrume.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,7 @@ extension CGSize {
}

extension UIView {

final func snapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0)
drawHierarchy(in: bounds, afterScreenUpdates: true)
let snapshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return snapshot!
}

func snapshotView() -> UIView? {
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, 0)
defer {
UIGraphicsEndImageContext()
}
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
layer.render(in: context)
return UIImageView(image: UIGraphicsGetImageFromCurrentImageContext())
}


func usesAutoLayout(_ useAutoLayout: Bool) {
translatesAutoresizingMaskIntoConstraints = !useAutoLayout
}
Expand Down
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,6 @@ let agrume = Agrume(dataSource: dataSource)
agrume.show(from: self)
```

### Custom Background Snapshot

When showing the Agrume view controller, it'll default to taking a snapshot of the root view and blurring that. You can customize this behaviour by passing in a different view that it will blur and display:

```swift
let agrume = Agrume(image: image)
agrume.show(from: self, backgroundSnapshotVC: self)
```

### Status Bar Appearance

You can customize the status bar appearance when displaying the zoomed in view. `Agrume` has a `statusBarStyle` property:
Expand Down

0 comments on commit 990e7c3

Please sign in to comment.