Sunday 11 June 2017

UIImagePickerController in Swift

In iOS app development UIImagePickerController is used to access the picture either from the camera or photos app. It's very easy to use UIImagePickerController. But what if we want to access image picker more than once in an iOS app. Here we don't want redundant code .

In swift it's very easy to write code which can be used all over the application. Yes the rescue is the extension. We can create an extension of UIViewController which can be called from every view controller where we want to present the image picker.

In iOS 10, first of all we have to declare the usage of camera and photos app in info.plist.

Then create an extension of UIViewController to present image picker:

 func imagePickerControllerWith(delegate:(UIImagePickerControllerDelegate & UINavigationControllerDelegate)?){
        let actionSheet =  UIAlertController(title: "YakYapp", message: "", preferredStyle: .actionSheet)
        
        let camera  = UIAlertAction(title: "Camera", style: .default) { (cameraAction) in
            
            let mediaType = AVMediaTypeVideo
            
            let authorizationStatus =  AVCaptureDevice.authorizationStatus(forMediaType: mediaType)
            
            if authorizationStatus == .authorized{
                self.prsentImagePickerWith(option: "Camera",delegate: delegate)
                
            }else if authorizationStatus == .notDetermined{
                AVCaptureDevice.requestAccess(forMediaType: mediaType, completionHandler: { (newStatus) in
                    if newStatus {
                        let authorizationStatus1 =  AVCaptureDevice.authorizationStatus(forMediaType: mediaType)
                        if authorizationStatus1 == .authorized{
                            self.prsentImagePickerWith(option: "Camera",delegate: delegate)
                        }
                        
                    }else{
                        
                    }
                })
            }else{
                
                let message = "It seems access for camera is denied for Spirosure. To make it work, Go to Setting -> Privacy -> Camera and enable the access for the app"
                self.showAlertWith(message: message, title: "", handler: nil)
            }
        }
        
        let gallery  = UIAlertAction(title: "Photo Album", style: .default) { (galleryActn) in
            
            let status = PHPhotoLibrary.authorizationStatus()
            
            switch status{
                
            case .authorized:
                self.prsentImagePickerWith(option: "PhotoAlbum",delegate: delegate)
            case .notDetermined:
                PHPhotoLibrary.requestAuthorization({ (newStatus) in
                    if newStatus == PHAuthorizationStatus.authorized{
                        self.prsentImagePickerWith(option: "PhotoAlbum",delegate: delegate)
                        
                    }else{
                        
                    }
                })
            default:
                self.showAlertWith(message: "The access is not granted to use photo library. Go to Setting app to change the access for the app.", title: "", handler: nil)
                
            }
        }
        
        let cancel  = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        
        actionSheet.addAction(camera)
        actionSheet.addAction(gallery)
        actionSheet.addAction(cancel)
        self.present(actionSheet, animated: true, completion: nil)
        
    }
    
   private func prsentImagePickerWith(option:String,delegate:(UIImagePickerControllerDelegate & UINavigationControllerDelegate)?){
        let uiimagePicker = UIImagePickerController.init()
        uiimagePicker.delegate = delegate
        
        if option == "Camera" && UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
            uiimagePicker.sourceType = .camera
        }else {
            uiimagePicker.sourceType = .photoLibrary
        }
        
        DispatchQueue.main.async {
            self.present(uiimagePicker, animated: true, completion: nil)
        }
        
    } 

Here we created an action sheet to let user select the source from which he/she will pick the image and then we check based on choice of user, whether this app has access to that source or not and if allowed then present the image picker.

To use in your view controller:

A) First set the viewcontroller  to implement the delegate for UIImagePickerControllerDelegate and UINavigationControllerDelegate

B) Then call method of extension to present an image picker from your view controller as:
        
      self.imagePickerControllerWith(delegate: self)

1 comment:

  1. Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best. Hire iPhone Developers India

    ReplyDelete