Swift: Add, Edit, Delete — part2
choose photo with photo album/ take picture with camera
App features
part1:
- add, edit, delete content → to record items in document directory or UserDefaults
- https://medium.com/@jieru04/swift-add-edit-delete-part1-651f6a50499e
part2:
- choose photo with photo album/ take picture with camera → UIImagePickerController
part3:
- choose a store and comment → UIPickerView
- pick date
- dismiss keyboard
- https://medium.com/@jieru04/swift-add-edit-delete-part3-6a0924075ca1
Select photo & Take picture
Tap Gesture Recognizer
to select photo or take picture.
- add
Tap Gesture Recognizer
on the UIImageView. - check
User Interaction Enabled
or write the UIImageView of instance property.isUserInteractionEnabled
.
3. alert message
- to create IBAction of tap gesture recognizer to select photo or take picture.
- user can choose one of options while tapping image.
//Tap Gesture選照片或拍照
@IBAction func selectPhoto(_ sender: UITapGestureRecognizer) {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)//選單樣式
let photoAction = UIAlertAction(title: "Choose photo", style: .default) { action in
self.selectphoto()
}
let cameraAction = UIAlertAction(title: "Take picture", style: .default) { action in
self.takePicture()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default)
alertController.addAction(photoAction)
alertController.addAction(cameraAction)
alertController.addAction(cancelAction)
present(alertController, animated: true)
}
3.1 conform protocol UIImagePickerControllerDelegate ,UINavigationControllerDelegate.
3.2 create function.
//MARK: - UIImagePickerControllerDelegate
extension EditTableViewController:UIImagePickerControllerDelegate ,UINavigationControllerDelegate{
//跳出相簿
func selectphoto(){
let imageContriller = UIImagePickerController()
imageContriller.sourceType = .photoLibrary
imageContriller.delegate = self
imageContriller.allowsEditing = true //選取後的照片是否能編緝
present(imageContriller, animated: true)
}
//選擇照片
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
isSelectedPhoto = true
//型態為 originaImage,其它型態有影片、修改過的圖片等等
let picture = info [UIImagePickerController.InfoKey.originalImage] as! UIImage //Any型別轉型成UIImage,才可將照片加到Imageview上
photoImageView.contentMode = .scaleAspectFit
photoImageView.image = picture
//照片存入相簿
UIImageWriteToSavedPhotosAlbum(picture, nil, nil, nil)
//選完照片後退掉畫面
dismiss(animated: true)
}
//拍照
func takePicture(){
let controller = UIImagePickerController()
controller.sourceType = .camera
controller.delegate = self
present(controller, animated: true)
}
}
Here function is selectphoto()
.
UIImagePickerController()
which manages the system interfaces for taking pictures, recording movies, and choosing items from the user’s media library.
choose picture:
imagePickerController(_:didFinishPickingMediaWithInfo:)
Tells the delegate that the user picked the image .
take picture:
Privacy settings
To allow this app to take picture and use photo from library.
To allow this app to save picture in the photo album.
Reference
UIImagePickerController
Save picture to photo album
Allow app to use camera and photo library