Skip to content

Commit

Permalink
feat: support to add custom actions for sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
janlionly committed Aug 10, 2020
1 parent f6298cb commit b27a86d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
8 changes: 4 additions & 4 deletions AvatarImagePicker.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|
s.name = 'AvatarImagePicker'
s.version = '1.3.0'
s.version = '1.3.1'
s.summary = 'A single line of code for selecting the optional editing image from Camera or Photo Library.'

s.description = <<-DESC
AvatarImagePicker is a photo library and camera Image Picker for iOS
written in Swift, it's just a single line of code, support for selecting user's avatar by Camera or Photo Library, editing the selected image. Also, it supports auth verification, if camera or photo library was denied, it will alert the user to the settings for opening it. it means to replace for UIImagePickerController. Compatible with both Swift and Objective-C.
DESC

s.homepage = 'https://github.com/janlionly/AvatarImagePicker'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'janlionly' => 'janlionly@gmail.com' }
Expand All @@ -19,4 +19,4 @@ written in Swift, it's just a single line of code, support for selecting user's
s.frameworks = 'UIKit', 'AVFoundation', 'Photos'
s.swift_versions = ['4.2', '5.0', '5.1', '5.2']
s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.2' }
end
end
9 changes: 4 additions & 5 deletions AvatarImagePickerDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097.2" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -20,6 +18,7 @@
<subviews>
<imageView clipsSubviews="YES" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="default_avatar" translatesAutoresizingMaskIntoConstraints="NO" id="bFO-gA-y8N">
<rect key="frame" x="147" y="144" width="120" height="120"/>
<color key="backgroundColor" systemColor="systemYellowColor" red="1" green="0.80000000000000004" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<gestureRecognizers/>
<constraints>
<constraint firstAttribute="width" constant="120" id="3Hx-gp-3P9"/>
Expand Down
7 changes: 6 additions & 1 deletion AvatarImagePickerDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ class ViewController: UIViewController {
// - MARK: Actions

@IBAction func imageViewTapped(_ sender: Any) {
AvatarImagePicker.instance.present(allowsEditing: true, selected: { (image) in
let picker = AvatarImagePicker.instance
picker.sourceTypes = [.camera, .customAction]
weak var weakSelf = self
picker.customActions = ["Delete": { weakSelf?.imageView.image = nil }]

picker.present(allowsEditing: true, selected: { (image) in
self.imageView.image = image
}) {
print("Tap cancel")
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ picker.present(allowsEditing: true, selected: { (image) in
}


// v1.3.1 updated: support to customize actions for sheet
picker.sourceTypes = [.camera, .customAction]
weak var weakSelf = self
picker.customActions = ["Delete": { weakSelf?.imageView.image = nil }]


// or you can call only auth photolibrary and camera, it will alert the user to go to settings if the photolibrary or camera was denied.
let isAuthSuccess = AuthSettings.authPhotoLibrary(message: "auth photolibrary to get your avatar") {
print("auth success")
Expand Down
49 changes: 32 additions & 17 deletions Sources/AvatarImagePicker/AvatarImagePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ open class AvatarImagePicker: NSObject, UIImagePickerControllerDelegate, UINavig
public enum SourceType {
case photoLibrary
case camera
case customAction
}
/// default both photolibrary and camera
public var sourceTypes: Set<SourceType> = [.photoLibrary, .camera]
public var presentStyle: UIModalPresentationStyle = .fullScreen
public var dismissAnimated: Bool = true
public var customActions: [String: ()->Void] = [:]

public static var instance: AvatarImagePicker {
if sharedInstance == nil {
Expand Down Expand Up @@ -151,27 +153,40 @@ open class AvatarImagePicker: NSObject, UIImagePickerControllerDelegate, UINavig

if sourceTypes.count > 1 {
let sheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
sheet.addAction(UIAlertAction(title: NSLocalizedString("Photo Library", comment: ""), style: .default, handler: { (_) in
self.imagePicker.sourceType = .photoLibrary
_ = AuthSettings.authPhotoLibrary(message: NSLocalizedString("Go to settings to authorize photo library", comment: ""), completion: {
DispatchQueue.main.async {
window.visibleViewController?.present(self.imagePicker, animated: true, completion: nil)
}
})

}))
sheet.addAction(UIAlertAction(title: NSLocalizedString("Camera", comment: ""), style: .default, handler: { (_) in
self.imagePicker.sourceType = .camera
_ = AuthSettings.authCamera(message: NSLocalizedString("Go to settings to authorize camera", comment: ""), completion: {
DispatchQueue.main.async {
window.visibleViewController?.present(self.imagePicker, animated: true, completion: nil)
}
})
}))

if sourceTypes.contains(.photoLibrary) {
sheet.addAction(UIAlertAction(title: NSLocalizedString("Photo Library", comment: ""), style: .default, handler: { (_) in
self.imagePicker.sourceType = .photoLibrary
_ = AuthSettings.authPhotoLibrary(message: NSLocalizedString("Go to settings to authorize photo library", comment: ""), completion: {
DispatchQueue.main.async {
window.visibleViewController?.present(self.imagePicker, animated: true, completion: nil)
}
})

}))
}

if sourceTypes.contains(.camera) {
sheet.addAction(UIAlertAction(title: NSLocalizedString("Camera", comment: ""), style: .default, handler: { (_) in
self.imagePicker.sourceType = .camera
_ = AuthSettings.authCamera(message: NSLocalizedString("Go to settings to authorize camera", comment: ""), completion: {
DispatchQueue.main.async {
window.visibleViewController?.present(self.imagePicker, animated: true, completion: nil)
}
})
}))
}

sheet.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: { (_) in
cancel?()
AvatarImagePicker.sharedInstance = nil
}))

for (key, value) in customActions {
sheet.addAction(UIAlertAction(title: key, style: .default, handler: { (_) in
value()
}))
}
present(for: sheet)
} else if let type = sourceTypes.first, type == .camera {
self.imagePicker.sourceType = .camera
Expand Down

0 comments on commit b27a86d

Please sign in to comment.