Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.67 KB | None | 0 0
  1. //
  2. //  CustomeCellPromo.swift
  3. //
  4. //  Created by Macbook pro on 8/29/17.
  5. //  Copyright © 2017 Macbook pro. All rights reserved.
  6. //
  7.  
  8. import UIKit
  9.  
  10. class CustomeCellPromo: UITableViewCell , UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  11.    
  12.     @IBOutlet weak var imagePromo: UIImageView!
  13.     var targetImageView:UIImageView?
  14.     var tableViewController:UITableViewController?
  15.    
  16.     override func awakeFromNib() {
  17.         super.awakeFromNib()
  18.        
  19.         let tap = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
  20.         tap.numberOfTapsRequired = 1
  21.         tap.numberOfTouchesRequired = 1
  22.        
  23.         imagePromo.addGestureRecognizer(tap)
  24.     }
  25.    
  26.     override func setSelected(_ selected: Bool, animated: Bool) {
  27.         super.setSelected(selected, animated: animated)
  28.     }
  29.    
  30.     func imageTapped(tapGestureRecognizer:UITapGestureRecognizer) {
  31.         let tappedImage = tapGestureRecognizer.view as? UIImageView
  32.         targetImageView = tappedImage!
  33.         getPicture()
  34.     }
  35.    
  36.    
  37.     // - Function to show alert how user choice way to give picture
  38.     func getPicture() {
  39.         let alert:UIAlertController = UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
  40.         let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) {
  41.             UIAlertAction in self.openCamera()
  42.         }
  43.         let galleryAction = UIAlertAction(title: "Gallery", style: UIAlertActionStyle.default) {
  44.             UIAlertAction in self.openGallery()
  45.         }
  46.         let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
  47.             UIAlertAction in
  48.         }
  49.        
  50.         alert.addAction(cameraAction)
  51.         alert.addAction(galleryAction)
  52.         alert.addAction(cancelAction)
  53.        
  54.         tableViewController?.present(alert, animated: true, completion: nil)
  55.     }
  56.    
  57.     func openCamera() {
  58.         print("Camera Opened")
  59.         // Check if phone has camera?
  60.         if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
  61.             let imagePicker = UIImagePickerController()
  62.             imagePicker.delegate = self
  63.             imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
  64.             imagePicker.allowsEditing = false
  65.            
  66.             tableViewController?.present(imagePicker, animated: true, completion: nil)
  67.         }
  68.     }
  69.    
  70.     func openGallery() {
  71.         print("Galery opened")
  72.        
  73.         if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary)) {
  74.             let imagePicker = UIImagePickerController()
  75.             imagePicker.delegate = self
  76.             imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
  77.             imagePicker.allowsEditing = false
  78.            
  79.             tableViewController?.present(imagePicker, animated: false, completion: nil)
  80.         }
  81.     }
  82.    
  83.     // Handle after user take pictue / chosse photo from library
  84.     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  85.         if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
  86.             imagePromo.image = image
  87.             tableViewController?.dismiss(animated: true, completion: nil)
  88.         }
  89.     }
  90.    
  91.     // Handle cancel user from take picture / choice photo from library
  92.     func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  93.        tableViewController?.dismiss(animated: true, completion: nil)
  94.     }
  95.    
  96.    
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement