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

Add crop function for multiple photo mode #566

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Source/Configuration/YPImagePickerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ public struct YPConfigLibrary {
/// Allow to skip the selections gallery when selecting the multiple media items. Defaults to false.
public var skipSelectionsGallery = false

/// Selection gallery navigationItem title. Defaults to "".
public var selectionsGalleryTitle = ""

/// Allow to preselected media items
public var preselectedItems: [YPMediaItem]?

Expand Down
3 changes: 3 additions & 0 deletions Source/Filters/Crop/YPCropVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ class YPCropVC: UIViewController {
let imageRef = cgImage.cropping(to: scaledCropRect) {
let croppedImage = UIImage(cgImage: imageRef)
didFinishCropping?(croppedImage)
if YPConfig.library.maxNumberOfItems > 1 {
navigationController?.popViewController(animated: true)
}
}
}
}
Expand Down
46 changes: 40 additions & 6 deletions Source/SelectionsGallery/YPSelectionsGalleryVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class YPSelectionsGalleryVC: UIViewController, YPSelectionsGalleryCellDel

var v = YPSelectionsGalleryView()
public override func loadView() { view = v }

public required init(items: [YPMediaItem],
didFinishHandler:
@escaping ((_ gallery: YPSelectionsGalleryVC, _ items: [YPMediaItem]) -> Void)) {
Expand All @@ -33,15 +33,16 @@ public class YPSelectionsGalleryVC: UIViewController, YPSelectionsGalleryCellDel

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

// Register collection view cell
v.collectionView.register(YPSelectionsGalleryCell.self, forCellWithReuseIdentifier: "item")
v.collectionView.dataSource = self
v.collectionView.delegate = self

// Setup navigation bar
navigationItem.title = YPConfig.library.selectionsGalleryTitle
navigationItem.rightBarButtonItem = UIBarButtonItem(title: YPConfig.wordings.next,
style: .done,
style: .plain,
target: self,
action: #selector(done))
navigationItem.rightBarButtonItem?.tintColor = YPConfig.colors.tintColor
Expand All @@ -52,7 +53,7 @@ public class YPSelectionsGalleryVC: UIViewController, YPSelectionsGalleryCellDel
YPHelper.changeBackButtonIcon(self)
YPHelper.changeBackButtonTitle(self)
}

@objc
private func done() {
// Save new images to the photo album.
Expand Down Expand Up @@ -86,14 +87,18 @@ extension YPSelectionsGalleryVC: UICollectionViewDataSource {
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "item",
for: indexPath) as? YPSelectionsGalleryCell else {
return UICollectionViewCell()
return UICollectionViewCell()
}
cell.delegate = self
let item = items[indexPath.row]
switch item {
case .photo(let photo):
cell.imageView.image = photo.image
cell.setEditable(YPConfig.showsPhotoFilters)
var showCrop = false
if case YPCropType.rectangle(_) = YPConfig.showsCrop {
showCrop = true
}
cell.setEditable(YPConfig.showsPhotoFilters || showCrop)
case .video(let video):
cell.imageView.image = video.thumbnail
cell.setEditable(YPConfig.showsVideoTrimmer)
Expand All @@ -112,6 +117,35 @@ extension YPSelectionsGalleryVC: UICollectionViewDelegate {
case .photo(let photo):
if !YPConfig.filters.isEmpty, YPConfig.showsPhotoFilters {
mediaFilterVC = YPPhotoFiltersVC(inputPhoto: photo, isFromSelectionVC: true)
} else {
let completion = { (photo: YPMediaPhoto) in
let mediaItem = YPMediaItem.photo(p: photo)
// Save new image or existing but modified, to the photo album.
if YPConfig.shouldSaveNewPicturesToAlbum {
let isModified = photo.modifiedImage != nil
if photo.fromCamera || (!photo.fromCamera && isModified) {
YPPhotoSaver.trySaveImage(photo.image, inAlbumNamed: YPConfig.albumName)
}
}
self.items[indexPath.row] = mediaItem
collectionView.reloadData()
}

func showCropVC(photo: YPMediaPhoto, completion: @escaping (_ aphoto: YPMediaPhoto) -> Void) {
if case let YPCropType.rectangle(ratio) = YPConfig.showsCrop {
let cropVC = YPCropVC(image: photo.originalImage, ratio: ratio)
cropVC.didFinishCropping = { croppedImage in
photo.modifiedImage = croppedImage
completion(photo)
}
self.show(cropVC, sender: self)
} else {
completion(photo)
}
}

showCropVC(photo: photo, completion: completion)
return
}
case .video(let video):
if YPConfig.showsVideoTrimmer {
Expand Down