Using UIImagePickerController.
imagePickerController.delegate = self
extension ImagesViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
return
}
selectedImage = image
dismiss(animated: true)
}
}
if UIImagePickerController.isSourceTypeAvailable(.camera) {
alertController.addAction(cameraAction)
}
If the NSCameraUsageDescription is not added in the info.plist the following crash will occur.
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.
extension UIImage {
func resizeImage(to width: CGFloat, height: CGFloat) -> UIImage {
let size = CGSize(width: width, height: height)
let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image { (context) in
self.draw(in: CGRect(origin: .zero, size: size))
}
}
}
import AVFoundation
let rect = AVMakeRect(aspectRatio: image.size, insideRect: UIScreen.main.bounds)
let imageData = resizedImage.jpegData(compressionQuality: 1.0)
Comes in very handy when conforming your object to hashable for uniqueness.
struct ImageObject: Codable {
let imageData: Data
let date: Date
let identifier = UUID().uuidString
}
private func showDeleteAlert(forCell indexPath: IndexPath) {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
let deleteAction = UIAlertAction(title: "Delete", style: .destructive) { [weak self] alertAction in
self?.deleteImageObject(indexPath: indexPath)
}
alertController.addAction(cancelAction)
alertController.addAction(deleteAction)
present(alertController, animated: true)
}
private lazy var longPressGesture: UILongPressGestureRecognizer = {
let gesture = UILongPressGestureRecognizer()
gesture.addTarget(self, action: #selector(longPress(gesture:)))
return gesture
}()
addGestureRecognizer(longPressGesture)
@objc
private func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
gesture.state = .cancelled
return
}
delegate?.didLongPressOnCell(self)
}