如何分别获取基于相机和基于媒体的图像/视频以在iOS设备的收藏视图中显示

问题描述:

我正在开发一个聊天应用程序。在我的应用程序中,当点击附件按钮时,应该有两个选项。如何分别获取基于相机和基于媒体的图像/视频以在iOS设备的收藏视图中显示

1)图像/由此时的器件照相机(未捕获图像捕获的视频。获取从网络或其它下载由存储在设备中的相机拍摄的图像)

2)图像/视频媒体

有什么办法最好使用资产库

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 

    } 
} 
+0

我不擅长迅速根据上面给出的标准来获取图像/视频。我从你的代码中了解到的是设备相机被访问用于拍照。但是我希望以前存储在设备中的相机拍摄的图像和来自媒体的图像是两种不同的实体,就像android手机中的一样。那可能吗? – Babybet

+0

如果你想使用android你需要使用filemanager,你只能使用图片库选择http://www.techotopia.com/index.php/Working_with_Files_in_Swift_on_iOS_8 –