0

I am developing a chat app. In my app when I click attachement button, two options should come.

1) images/videos captured by the device camera(not capturing image at that time. Fetch images taken by the camera that is stored in the device)

2) images/videos downloaded from the web or other medias

Is there any way to fetch images/videos according to the above given criteria preferably using assets library

Betsy
  • 219
  • 3
  • 10

1 Answers1

0
class YourController :  UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate,UIPopoverPresentationControllerDelegate
{    

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func takePhotoByGalleryOrCamera(){

        //MAark Take picture from gallery and camera 
        //image picker controller to use take image 
        // uialert controller to make action

        let imageController = UIImagePickerController()
        imageController.editing = false
        imageController.delegate = self;

        let alert = UIAlertController(title: "", message: "Profile Image Selctor", preferredStyle: UIAlertControllerStyle.ActionSheet)


        let libButton = UIAlertAction(title: "Select photo from library", style: UIAlertActionStyle.Default) { (alert) -> Void in
            imageController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            self.presentViewController(imageController, animated: true, completion: nil)
        }


        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
            let cameraButton = UIAlertAction(title: "Take a picture", style: UIAlertActionStyle.Default) { (alert) -> Void in
                print("Take Photo")
                imageController.sourceType = UIImagePickerControllerSourceType.Camera
                self.presentViewController(imageController, animated: true, completion: nil)

            }
            alert.addAction(cameraButton)
        } else {
            print("Camera not available")

        }
        let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (alert) -> Void in
            print("Cancel Pressed")
        }

        alert.addAction(libButton)
        alert.addAction(cancelButton)

        if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad {

            alert.modalPresentationStyle = UIModalPresentationStyle.Popover;
            //        alert.transitioningDelegate = self;
            alert.popoverPresentationController!.sourceView = self.view;
            alert.popoverPresentationController!.sourceRect = CGRectMake(0, SizeUtil.screenHeight(), SizeUtil.screenWidth(), SizeUtil.screenHeight()*0.4)
            alert.popoverPresentationController!.delegate = self;

            self.presentViewController(alert, animated: true, completion: nil)
        } else {
           self.presentViewController(alert, animated: true, completion: nil)
        }



    }

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {

        return UIModalPresentationStyle.Popover
    }


    //image picker Delegate
    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        self.dismissViewControllerAnimated(true, completion: nil)

        let header = profileTableView.headerViewForSection(0) as! ProfileHeaderView
        header.btnImage.setImage(image, forState: UIControlState.Normal)
        _userProfileImage = image

    }
}
Muhammad Noman
  • 1,566
  • 1
  • 15
  • 21
  • I'm not good at swift. What I understood from your code is device camera is accessed for taking pics. But I want the images taken by the camera earlier that is stored in the device and the images from media as two different entities like in android phones. Is that possible? – Betsy Mar 01 '16 at 07:38
  • you can only use pick by gallery if you want like android you need to use filemanager http://www.techotopia.com/index.php/Working_with_Files_in_Swift_on_iOS_8 – Muhammad Noman Mar 01 '16 at 08:07