Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tankista committed Oct 13, 2017
2 parents 85bc775 + 78f7a27 commit cdf8326
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ExampleApp/Custom Views/CameraCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CameraCell : CameraCollectionViewCell {
}

@IBAction func flipButtonTapped(_ sender: UIButton) {
flipCamera(nil)
flipCamera()
}

}
2 changes: 1 addition & 1 deletion ExampleApp/Custom Views/LivePhotoCameraCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LivePhotoCameraCell : CameraCollectionViewCell {
}

@IBAction func flipButtonTapped(_ sender: UIButton) {
flipCamera(nil)
flipCamera()
}

// MARK: Override Methods
Expand Down
2 changes: 1 addition & 1 deletion ExampleApp/Custom Views/VideoCameraCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class VideoCameraCell : CameraCollectionViewCell {
}

@IBAction func flipButtonTapped(_ sender: UIButton) {
flipCamera(nil)
flipCamera()
}

// MARK: Override Methods
Expand Down
4 changes: 2 additions & 2 deletions ExampleApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let cellsData: [[CellData]] = [
CellData("Disabled", #selector(ViewController.configCameraItem(indexPath:)), .indexPath, { cell, controller in cell.accessoryType = controller.cameraConfig == .disabled ? .checkmark : .none })
],
[
CellData("Recently added (default)", #selector(ViewController.configAssetsSource(indexPath:)), .indexPath, { cell, controller in cell.accessoryType = controller.assetsSource == .recentlyAdded ? .checkmark : .none }),
CellData("Camera Roll (default)", #selector(ViewController.configAssetsSource(indexPath:)), .indexPath, { cell, controller in cell.accessoryType = controller.assetsSource == .recentlyAdded ? .checkmark : .none }),
CellData("Only videos", #selector(ViewController.configAssetsSource(indexPath:)), .indexPath, { cell, controller in cell.accessoryType = controller.assetsSource == .onlyVideos ? .checkmark : .none }),
CellData("Only selfies", #selector(ViewController.configAssetsSource(indexPath:)), .indexPath, { cell, controller in cell.accessoryType = controller.assetsSource == .onlySelfies ? .checkmark : .none })
],
Expand Down Expand Up @@ -269,7 +269,7 @@ class ViewController: UITableViewController {
navigationController?.visibleViewController?.navigationItem.setRightBarButton(UIBarButtonItem(title: title, style: .plain, target: nil, action: nil), animated: true)
}
}

}

extension ViewController : ImagePickerControllerDelegate {
Expand Down
10 changes: 5 additions & 5 deletions ImagePicker/CameraCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,29 @@ open class CameraCollectionViewCell : UICollectionViewCell {
///
/// - parameter completion: A block is called as soon as camera is changed.
///
public func flipCamera(_ completion: (() -> Void)?) {
@objc public func flipCamera(_ completion: (() -> Void)? = nil) {
delegate?.flipCamera(completion)
}

///
/// Takes a picture
///
public func takePicture() {
@objc public func takePicture() {
delegate?.takePicture()
}

///
/// Takes a live photo. Please note that live photos must be enabled when configuring Image Picker.
///
public func takeLivePhoto() {
@objc public func takeLivePhoto() {
delegate?.takeLivePhoto()
}

public func startVideoRecording() {
@objc public func startVideoRecording() {
delegate?.startVideoRecording()
}

public func stopVideoRecording() {
@objc public func stopVideoRecording() {
delegate?.stopVideoRecording()
}

Expand Down
8 changes: 4 additions & 4 deletions ImagePicker/ImagePickerAssetModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ final class ImagePickerAssetModel {
lazy var imageManager = PHCachingImageManager()
var thumbnailSize: CGSize?

/// Tryies to access smart album recently added and uses just fetchAssets as fallback
/// Tryies to access smart album .smartAlbumUserLibrary that should be `Camera Roll` and uses just fetchAssets as fallback
private lazy var defaultFetchResult: PHFetchResult<PHAsset> = {

let assetsOptions = PHFetchOptions()
assetsOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
assetsOptions.fetchLimit = 1000

let collections = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumRecentlyAdded, options: nil)
if let recentlyAdded = collections.firstObject {
return PHAsset.fetchAssets(in: recentlyAdded, options: assetsOptions)
let collections = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: nil)
if let cameraRoll = collections.firstObject {
return PHAsset.fetchAssets(in: cameraRoll, options: assetsOptions)
}
else {
return PHAsset.fetchAssets(with: assetsOptions)
Expand Down
25 changes: 25 additions & 0 deletions ImagePicker/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ open class ImagePickerController : UIViewController {
///
public weak var dataSource: ImagePickerControllerDataSource?

///
/// Programatically select asset.
///
public func selectAsset(at index: Int, animated: Bool, scrollPosition: UICollectionViewScrollPosition) {
let path = IndexPath(item: index, section: layoutConfiguration.sectionIndexForAssets)
collectionView.selectItem(at: path, animated: animated, scrollPosition: scrollPosition)
}

///
/// Programatically deselect asset.
///
public func deselectAsset(at index: Int, animated: Bool) {
let path = IndexPath(item: index, section: layoutConfiguration.sectionIndexForAssets)
collectionView.deselectItem(at: path, animated: animated)
}

///
/// Programatically deselect all selected assets.
///
public func deselectAllAssets(animated: Bool) {
for selectedPath in collectionView.indexPathsForSelectedItems ?? [] {
collectionView.deselectItem(at: selectedPath, animated: animated)
}
}

///
/// Access all currently selected images
///
Expand Down
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ Various kind of configuration is supported. All configuration should be done **b
- to use your custom views for action, camera and asset items use `CellRegistrator` class
- don't forget to set your `delegate` and `dataSource` if needed
- to define a source of photos that should be available to pick up use view controller's `assetsFetchResultBlock` block
- to access selected assets use `selectedAssets` array

### Capture settings

Expand All @@ -86,7 +85,7 @@ Please refer to `CaptureSettings` public header for more information.

### Providing your own photos fetch result

By default Image Picker fetches from Photo Library 1000 recently added photos and videos. If you wish to provide your own fetch result please implement image picker controller's `assetsFetchResultBlock` block.
By default Image Picker fetches from Photo Library 1000 photos and videos from smart album `smartAlbumUserLibrary` that should represent *Camera Roll* album. If you wish to provide your own fetch result please implement image picker controller's `assetsFetchResultBlock` block.

For example to fetch only live photos you can use following code snippet:

Expand Down Expand Up @@ -226,7 +225,9 @@ func imagePicker(controller: ImagePickerController, didSelectActionItemAt index:

Image picker provides a default camera cell that just shows a camera output and captures a photo when user taps it.

If you wish to implement fancier features you must provide your own subclass of `CameraCollectionViewCell` and implement dedicated methods.
If you wish to implement fancier features you must provide your own subclass of `CameraCollectionViewCell` or nib file with custom cell class subclassing it and implement dedicated methods.

> Note: Please note, that custom nib's cell class must inherit from `CameraCollectionViewCell` and must not specify any reuse identifer. Image Picker is handling reuse identifiers internally.
Supported features of whoose UI can be fully customized:
- [x] taking photos, live photos, recording videos, flipping camera
Expand Down Expand Up @@ -304,6 +305,16 @@ To see an example how to set up Image Picker as an input view of a view controll

Optionaly, before presenting image picker, you can check if user has granted access permissions to Photos Library using `PHPhotoLibrary` API and ask for permissions. If you don't do it, image picker will take care of this automatically for you *after* it's presented.

## Accessing, selecting and deselecting asset items

Image Picker has several convinience methods to work with asset items.

- `selectedAssets` property returns an array of currently selected `PHAsset` items
- to access asset items at certain indexes, use `assets(at:)` and `asset(at:)`
- to programatically select an asset item use `selectAsset(at:animated:scrollPosition:)`
- to programatically deselect an asset item use `deselectAsset(at:animated:)`
- to programatically deselect all selected items use `deselectAllAssets(_:)`

## Features to add

1. landscape layout for camera cell - video is already in landscape but cell must be wider to properly display it
Expand Down

0 comments on commit cdf8326

Please sign in to comment.