Hi All,
Sometime we have to pick an image/video from device gallery and to open gallery/photos of your device and pick any image , we have to follow these simple steps.
 
1) Ask permission to access user gallery. Add these key and value in your project plist:-
 <key>NSPhotoLibraryUsageDescription</key>
 <string>This app requires access to the photo library.</string>
 
2) Add code to open image gallery:-
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){
            
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
        }
 
3) Last step to use the chose image by user :-
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        print(info)
//you can use image in data variable as this image is one which user picked from gallery. 
        let data = UIImagePNGRepresentation(info[UIImagePickerControllerOriginalImage] as! UIImage)
        
    }
 
                       
                    
0 Comment(s)