Skip to content

Instantly share code, notes, and snippets.

func blurFaces(in image: UIImage) -> UIImage? {
guard let ciImage = CIImage(image: image)?
.oriented(.init(image.imageOrientation)) else {
return nil
}
// this image will use as the "mask map"
var maskCanvasImage = CIImage.empty()
.cropped(to: ciImage.extent)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true)
guard let image = info[.originalImage] as? UIImage else {
return
}
imageView.image = image
// layout logic, fit image view size to image ratio...
...
extension CGImagePropertyOrientation {
init(_ uiOrientation: UIImage.Orientation) {
switch uiOrientation {
case .up: self = .up
case .upMirrored: self = .upMirrored
case .down: self = .down
case .downMirrored: self = .downMirrored
case .left: self = .left
case .leftMirrored: self = .leftMirrored
case .right: self = .right
func getFaceRects(in image: UIImage, normalizedTo rect: CGRect) -> [CGRect] {
guard var ciImage = CIImage(image: image) else {
print("Couldn't create CIImage.")
return []
}
// correct the orientation becuase `CIImage.init` can lose it
ciImage = ciImage.oriented(.init(image.imageOrientation))
let scaleFactorX = rect.width / ciImage.extent.width
import UIKit
class ImageProcessingService {
private let ciContext = CIContext()
private lazy var ciDetector = CIDetector(ofType: CIDetectorTypeFace, context: ciContext)!
}
import UIKit
import Combine
class LibraryViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
private var tokens = [AnyCancellable]()
// We're making the viewModel property lazy so it can access `self` during initialization.
private lazy var viewModel = LibraryViewModel(collectionView: collectionView)
@eilonkr
eilonkr / LibraryViewModel.swift
Last active November 30, 2020 09:29
LibraryViewModel.swift
class LibraryViewModel: CollectionViewModel<NoteCell> {
init(collectionView: UICollectionView) {
super.init(collectionView: collectionView, cellReuseIdentifier: "NoteCell")
}
}
// MARK: - UICollectionViewDelegate Implementation
extension LibraryViewModel: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
private func update() {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.main])
snapshot.appendItems(items.value)
dataSource?.apply(snapshot)
}
public func add(_ items: [Item]) {
items.forEach {
self.items.value.append($0)
// MARK: - Setup Logic
extension CollectionViewModel {
private func cellProvider(_ collectionView: UICollectionView, indexPath: IndexPath, item: Item) -> UICollectionViewCell? {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! CellType
cell.provide(item)
return cell
}
public func makeDataSource() -> DataSource {
enum Section {
case main
}