Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds cropping #8

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Sharaku (1.0.1)
- Sharaku (1.0.2)

DEPENDENCIES:
- Sharaku (from `../`)
Expand All @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
Sharaku: de44ee4ade4e6f4f1bcd0d86d9daaa88101c89b2
Sharaku: 9a0067b35337cecbdb0efb44f05680b3e46388e4

PODFILE CHECKSUM: ef1eae032f35d1ec3a285bf6b0259e2f3f3aa7de

COCOAPODS: 1.2.0.beta.3
COCOAPODS: 1.2.0
18 changes: 10 additions & 8 deletions Example/Sharaku.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = 48RUGN3XS3;
DevelopmentTeam = 9DE4B762BF;
LastSwiftMigration = 0820;
};
607FACE41AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = 48RUGN3XS3;
DevelopmentTeam = 9DE4B762BF;
LastSwiftMigration = 0820;
TestTargetID = 607FACCF1AFB9204008FA782;
};
Expand Down Expand Up @@ -443,7 +443,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.3;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -481,7 +481,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.3;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
Expand All @@ -493,8 +493,9 @@
baseConfigurationReference = E335AB8B6641B97B41E056F1 /* Pods-Sharaku_Example.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 48RUGN3XS3;
DEVELOPMENT_TEAM = 9DE4B762BF;
INFOPLIST_FILE = Sharaku/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
Expand All @@ -508,8 +509,9 @@
baseConfigurationReference = 792FD7528EB182F6B9C2A50F /* Pods-Sharaku_Example.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 48RUGN3XS3;
DEVELOPMENT_TEAM = 9DE4B762BF;
INFOPLIST_FILE = Sharaku/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
Expand All @@ -522,7 +524,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 655D612BB2C22B50F6C183AC /* Pods-Sharaku_Tests.debug.xcconfig */;
buildSettings = {
DEVELOPMENT_TEAM = 48RUGN3XS3;
DEVELOPMENT_TEAM = 9DE4B762BF;
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -543,7 +545,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = C975C2F8E6ECB3EE8B22CDCA /* Pods-Sharaku_Tests.release.xcconfig */;
buildSettings = {
DEVELOPMENT_TEAM = 48RUGN3XS3;
DEVELOPMENT_TEAM = 9DE4B762BF;
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
Expand Down
8 changes: 8 additions & 0 deletions Example/Sharaku/Images.xcassets/sample.imageset/Contents.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
"idiom" : "universal",
"filename" : "sample.jpg",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
Expand Down
20 changes: 4 additions & 16 deletions Example/Sharaku/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,13 @@ class ViewController: UIViewController {
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func sharakuButtonTapped(_ sender: Any) {
let image = UIImage(named: "sample")
let vc = SHViewController(image: image!)
vc.delegate = self
let vc = ContainerViewController(image: image!)
present(vc, animated: true, completion: nil)
}
}

extension ViewController: SHViewControllerDelegate {
func shViewControllerImageDidFilter(image: UIImage) {
imageView.image = image
showSharokuButton.isHidden = true
}

func shViewControllerDidCancel() {
vc.didFinishWithImage = { [weak self] image in
self?.imageView.image = image
}
}
}
2 changes: 1 addition & 1 deletion Sharaku.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Pod::Spec.new do |s|
s.author = { 'makomori' => '[email protected]' }
s.source = { :git => 'https://github.com/makomori/Sharaku.git', :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/makomori26'
s.ios.deployment_target = '8.3'
s.ios.deployment_target = '9.0'
s.source_files = 'Sharaku/Classes/**/*'
s.resources = ['Sharaku/Classes/**/*.xib', 'Sharaku/**/*.xcassets']
s.resource_bundles = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "PhotoCropEditorBorder.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
179 changes: 179 additions & 0 deletions Sharaku/Classes/ContainerViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import UIKit

extension UIView {
func add(view: UIView) {
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
}
func add(views: [UIView]) {
views.forEach{ add(view: $0) }
}
}

extension UIViewController {
func removeFromContainer() {
guard parent != nil else { return }
willMove(toParentViewController: nil)
view.removeFromSuperview()
removeFromParentViewController()
}

func add(to viewController: UIViewController, container: UIView) {
viewController.addChildViewController(self)
container.add(view: view)
NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: container.topAnchor),
view.leftAnchor.constraint(equalTo: container.leftAnchor),
view.rightAnchor.constraint(equalTo: container.rightAnchor),
view.bottomAnchor.constraint(equalTo: container.bottomAnchor),
])
}
}

public final class ContainerViewController: UIViewController {
public var didFinishWithImage: (UIImage) -> Void = { _ in }
public var didCancel: () -> Void = { _ in }
private let filterViewController: SHViewController
private let cropViewController: CropViewController
private let containerView = UIView()
private let filter: UIButton = {
let b = UIButton()
b.setTitleColor(.gray, for: .normal)
b.setTitleColor(.black, for: .selected)
b.setTitle("Filter", for: .normal)
return b
}()
private let crop: UIButton = {
let b = UIButton()
b.setTitleColor(.gray, for: .normal)
b.setTitleColor(.black, for: .selected)
b.setTitle("Crop", for: .normal)
return b
}()

public init(image: UIImage) {
filterViewController = SHViewController(image: image)
cropViewController = CropViewController(image: image)
super.init(nibName: nil, bundle: nil)
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

public override var prefersStatusBarHidden: Bool {
return true
}

override public func loadView() {
super.loadView()

let topBar = UIView()
topBar.backgroundColor = .white
view.add(view: topBar)

let next = UIButton()
next.setTitleColor(.black, for: .normal)
next.setTitle("Next", for: .normal)
next.addTarget(self, action: #selector(ContainerViewController.didTapNext), for: .touchUpInside)

let back = UIButton()
back.setTitleColor(.black, for: .normal)
back.setTitle("Back", for: .normal)
back.addTarget(self, action: #selector(ContainerViewController.didTapBack), for: .touchUpInside)

topBar.add(views: [next, back])

NSLayoutConstraint.activate([
topBar.leftAnchor.constraint(equalTo: view.leftAnchor),
topBar.rightAnchor.constraint(equalTo: view.rightAnchor),
topBar.topAnchor.constraint(equalTo: view.topAnchor),
topBar.heightAnchor.constraint(equalToConstant: 44),

next.rightAnchor.constraint(equalTo: topBar.rightAnchor, constant: -10),
next.topAnchor.constraint(equalTo: topBar.topAnchor),
next.bottomAnchor.constraint(equalTo: topBar.bottomAnchor),

back.leftAnchor.constraint(equalTo: topBar.leftAnchor, constant: 10),
back.topAnchor.constraint(equalTo: topBar.topAnchor),
back.bottomAnchor.constraint(equalTo: topBar.bottomAnchor)
])

let bottomTab = UIView()
bottomTab.backgroundColor = .white
view.add(view: bottomTab)

bottomTab.add(views: [filter, crop])

NSLayoutConstraint.activate([
bottomTab.leftAnchor.constraint(equalTo: view.leftAnchor),
bottomTab.rightAnchor.constraint(equalTo: view.rightAnchor),
bottomTab.bottomAnchor.constraint(equalTo: view.bottomAnchor),
bottomTab.heightAnchor.constraint(equalToConstant: 44),

filter.leftAnchor.constraint(equalTo: bottomTab.leftAnchor),
filter.bottomAnchor.constraint(equalTo: bottomTab.bottomAnchor),
filter.topAnchor.constraint(equalTo: bottomTab.topAnchor),
filter.rightAnchor.constraint(equalTo: bottomTab.centerXAnchor),

crop.leftAnchor.constraint(equalTo: bottomTab.centerXAnchor),
crop.bottomAnchor.constraint(equalTo: bottomTab.bottomAnchor),
crop.topAnchor.constraint(equalTo: bottomTab.topAnchor),
crop.rightAnchor.constraint(equalTo: bottomTab.rightAnchor),
])

view.add(view: containerView)

NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: topBar.bottomAnchor),
containerView.leftAnchor.constraint(equalTo: view.leftAnchor),
containerView.rightAnchor.constraint(equalTo: view.rightAnchor),
containerView.bottomAnchor.constraint(equalTo: bottomTab.topAnchor),
])
}

public override func viewDidLoad() {
super.viewDidLoad()
showFilter()
filter.addTarget(self, action: #selector(ContainerViewController.showFilter), for: .touchUpInside)
crop.addTarget(self, action: #selector(ContainerViewController.showCrop), for: .touchUpInside)
}

func showFilter() {
if let image = cropViewController.croppedImage {
filterViewController.image = image
}
cropViewController.removeFromContainer()
filterViewController.add(to: self, container: containerView)
filter.isSelected = true
crop.isSelected = false
}

func showCrop() {
filterViewController.removeFromContainer()
cropViewController.add(to: self, container: containerView)
filter.isSelected = false
crop.isSelected = true
}

func didTapNext() {
func sendImageFromFilter() {
guard let image = filterViewController.imageView?.image else {
assertionFailure()
return
}
didFinishWithImage(image)
}
if cropViewController.parent == nil {
sendImageFromFilter()
} else {
filterViewController.image = cropViewController.croppedImage
sendImageFromFilter()
}
dismiss(animated: true, completion: nil)
}

func didTapBack() {
didCancel()
}
}
Loading