Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // CustomeCellPromo.swift
- //
- // Created by Macbook pro on 8/29/17.
- // Copyright © 2017 Macbook pro. All rights reserved.
- //
- import UIKit
- class CustomeCellPromo: UITableViewCell , UIImagePickerControllerDelegate, UINavigationControllerDelegate {
- @IBOutlet weak var imagePromo: UIImageView!
- var targetImageView:UIImageView?
- var tableViewController:UITableViewController?
- override func awakeFromNib() {
- super.awakeFromNib()
- let tap = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
- tap.numberOfTapsRequired = 1
- tap.numberOfTouchesRequired = 1
- imagePromo.addGestureRecognizer(tap)
- }
- override func setSelected(_ selected: Bool, animated: Bool) {
- super.setSelected(selected, animated: animated)
- }
- func imageTapped(tapGestureRecognizer:UITapGestureRecognizer) {
- let tappedImage = tapGestureRecognizer.view as? UIImageView
- targetImageView = tappedImage!
- getPicture()
- }
- // - Function to show alert how user choice way to give picture
- func getPicture() {
- let alert:UIAlertController = UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
- let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) {
- UIAlertAction in self.openCamera()
- }
- let galleryAction = UIAlertAction(title: "Gallery", style: UIAlertActionStyle.default) {
- UIAlertAction in self.openGallery()
- }
- let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
- UIAlertAction in
- }
- alert.addAction(cameraAction)
- alert.addAction(galleryAction)
- alert.addAction(cancelAction)
- tableViewController?.present(alert, animated: true, completion: nil)
- }
- func openCamera() {
- print("Camera Opened")
- // Check if phone has camera?
- if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
- let imagePicker = UIImagePickerController()
- imagePicker.delegate = self
- imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
- imagePicker.allowsEditing = false
- tableViewController?.present(imagePicker, animated: true, completion: nil)
- }
- }
- func openGallery() {
- print("Galery opened")
- if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary)) {
- let imagePicker = UIImagePickerController()
- imagePicker.delegate = self
- imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
- imagePicker.allowsEditing = false
- tableViewController?.present(imagePicker, animated: false, completion: nil)
- }
- }
- // Handle after user take pictue / chosse photo from library
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
- if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
- imagePromo.image = image
- tableViewController?.dismiss(animated: true, completion: nil)
- }
- }
- // Handle cancel user from take picture / choice photo from library
- func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
- tableViewController?.dismiss(animated: true, completion: nil)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement