Skip to content

Commit

Permalink
Allow picking image from photo album or camera.
Browse files Browse the repository at this point in the history
  • Loading branch information
GetToSet committed Feb 11, 2020
1 parent 1358ca4 commit c0cb6e4
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 13 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ xcuserdata/
## Playgrounds
timeline.xctimeline
playground.xcworkspace

## JetBrains AppCode

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/AsciiFan.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/runConfigurations/AsciiFan.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/runConfigurations/Book_Sources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/runConfigurations/LiveViewTestApp.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/xcode.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions AsciiFan.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
71EDDEAA23F2635A005EBB8A /* lantern.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 71EDDEA023F2635A005EBB8A /* lantern.jpg */; };
71EDDEAB23F2635A005EBB8A /* desert.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 71EDDEA123F2635A005EBB8A /* desert.jpg */; };
71F704A723F2E2AC00D92D90 /* tick.png in Resources */ = {isa = PBXBuildFile; fileRef = 71F704A623F2E2AC00D92D90 /* tick.png */; };
71F704A923F2EAFB00D92D90 /* picker-camera.png in Resources */ = {isa = PBXBuildFile; fileRef = 71F704A823F2EAFB00D92D90 /* picker-camera.png */; };
B99F60C9EAB0D09A7B8EB0CC /* HistogramAndEqualizationLiveViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B99F6C6389EB17DF9F954711 /* HistogramAndEqualizationLiveViewController.swift */; };
B99F61D5DF56B9B158F4BFA6 /* HowImagesComposedLiveViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B99F6E315E7424BEA2E2B15F /* HowImagesComposedLiveViewController.swift */; };
B99F61E8920140B9CB82E17C /* ImagePickerCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B99F6A9BFBE7AF37265A3E19 /* ImagePickerCollectionViewCell.swift */; };
Expand Down Expand Up @@ -160,6 +161,7 @@
71EDDEA023F2635A005EBB8A /* lantern.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = lantern.jpg; sourceTree = "<group>"; };
71EDDEA123F2635A005EBB8A /* desert.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = desert.jpg; sourceTree = "<group>"; };
71F704A623F2E2AC00D92D90 /* tick.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = tick.png; sourceTree = "<group>"; };
71F704A823F2EAFB00D92D90 /* picker-camera.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "picker-camera.png"; sourceTree = "<group>"; };
B99F613FABA78624AAA4B93B /* ImagePickerDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePickerDataSource.swift; sourceTree = "<group>"; };
B99F61FAC3DE1254ACABCA7B /* MoreToPlayLiveViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MoreToPlayLiveViewController.swift; sourceTree = "<group>"; };
B99F649B4ABA6549FADE9BC3 /* IntroductionLiveViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntroductionLiveViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -325,6 +327,7 @@
71B1BC7023F15EF800F87CC1 /* PrivateResources */ = {
isa = PBXGroup;
children = (
71F704A823F2EAFB00D92D90 /* picker-camera.png */,
71F704A623F2E2AC00D92D90 /* tick.png */,
71747ABD23F2CC8F00F75DD4 /* tool-button-blue.png */,
71747ABF23F2CC9000F75DD4 /* tool-button-green.png */,
Expand Down Expand Up @@ -538,6 +541,7 @@
71EDDEA523F2635A005EBB8A /* lantern-thumb.jpg in Resources */,
5E086DCA2051DDAE004D8D25 /* LiveView.storyboard in Resources */,
71EDDE9723F252B8005EBB8A /* imagePickerList.plist in Resources */,
71F704A923F2EAFB00D92D90 /* picker-camera.png in Resources */,
71EDDEA823F2635A005EBB8A /* woman-thumb.jpg in Resources */,
71EDDEA423F2635A005EBB8A /* strawberries-thumb.jpg in Resources */,
71EDDEA923F2635A005EBB8A /* woman.jpg in Resources */,
Expand Down
2 changes: 1 addition & 1 deletion AsciiFan/PrivateResources/Base.lproj/LiveView.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
<size key="itemSize" width="64" height="64"/>
<size key="headerReferenceSize" width="0.0" height="0.0"/>
<size key="footerReferenceSize" width="0.0" height="0.0"/>
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="12" maxY="0.0"/>
</collectionViewFlowLayout>
<cells>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="imagePickerCollectionViewCell" id="3u7-gZ-EDS" customClass="ImagePickerCollectionViewCell" customModule="Book_Sources">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,19 @@ public class HowImagesComposedViewController: UIViewController, PlaygroundLiveVi

extension HowImagesComposedViewController: UICollectionViewDelegate, UICollectionViewDataSource, ImagePickerCollectionViewCellDelegate {

public func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.imageNames.count
switch section {
case 0:
return 1
case 1:
return dataSource.imageNames.count
default:
return 0
}
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
Expand All @@ -39,24 +50,89 @@ extension HowImagesComposedViewController: UICollectionViewDelegate, UICollectio
fatalError("Unexpected cell type")
}
cell.delegate = self
if let selectedCellIndexPath = selectedCellIndexPath,
indexPath == selectedCellIndexPath {
cell.state = .selected
} else {
cell.state = .normal

switch indexPath.section {
case 0:
cell.setImage(named: "picker-camera")
case 1:
if indexPath == selectedCellIndexPath {
cell.state = .selected
} else {
cell.state = .normal
}
let imageThumbnailName = dataSource.imageNames[indexPath.row].thumbnailName
cell.setImage(named: imageThumbnailName)
default:
break
}
let imageThumbnailName = dataSource.imageNames[indexPath.row].thumbnailName
cell.setImage(named: imageThumbnailName)
return cell
}

func thumbnailButtonTapped(cell: ImagePickerCollectionViewCell) {
if let indexPath = imagePickerCollectionView.indexPath(for: cell) {
selectedCellIndexPath = indexPath
let imageName = dataSource.imageNames[indexPath.row].fullImageName
imageView.image = UIImage(named: imageName)
imagePickerCollectionView.reloadData()
switch indexPath.section {
case 0:
showImagePicker(popoverSourceView: cell)
case 1:
selectedCellIndexPath = indexPath
let imageName = dataSource.imageNames[indexPath.row].fullImageName
imageView.image = UIImage(named: imageName)
imagePickerCollectionView.reloadData()
default:
break
}
}
}

}

extension HowImagesComposedViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

func showImagePicker(popoverSourceView: UIView) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self

let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

alert.addAction(UIAlertAction(title: "Take a Photo", style: .default, handler: { _ in
self.openCamera(pickerController: imagePickerController)
}))

alert.addAction(UIAlertAction(title: "Choose from Album", style: .default, handler: { _ in
self.openGallery(pickerController: imagePickerController)
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

alert.popoverPresentationController?.sourceView = popoverSourceView
alert.popoverPresentationController?.sourceRect = popoverSourceView.bounds
alert.popoverPresentationController?.permittedArrowDirections = .down

self.present(alert, animated: true)
}

func openCamera(pickerController: UIImagePickerController) {
guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
let alert = UIAlertController(title: "Warning", message: "You don't have a camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true)
return
}
pickerController.sourceType = .camera
self.present(pickerController, animated: true)
}

func openGallery(pickerController: UIImagePickerController) {
pickerController.sourceType = .photoLibrary
self.present(pickerController, animated: true)
}

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
imageView.image = selectedImage
}
picker.dismiss(animated: true)
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ImagePickerCollectionViewCell: UICollectionViewCell {
contentContainerView.layer.cornerRadius = 4.0

contentContainerView.layer.borderWidth = 3.0

updateAppearance()
}

func setImage(named imageName: String) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c0cb6e4

Please sign in to comment.