【问题标题】:How to fetch camera based and media based images/videos separately to display in collection view in iOS device如何分别获取基于相机和基于媒体的图像/视频以显示在 iOS 设备的集合视图中
【发布时间】:2016-06-13 11:34:51
【问题描述】:

我正在开发一个聊天应用程序。在我的应用程序中,当我单击附件按钮时,应该会出现两个选项。

1) 设备摄像头拍摄的图像/视频(当时不拍摄图像。获取存储在设备中的摄像头拍摄的图像)

2) 从网络或其他媒体下载的图像/视频

有没有办法根据上述给定的标准获取图像/视频,最好使用资产库

【问题讨论】:

    标签: ios objective-c uiimagepickercontroller alassetslibrary photokit


    【解决方案1】:
    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
    
        }
    }
    

    【讨论】:

    • 我不擅长 swift。我从您的代码中了解到的是可以访问设备相机来拍照。但我希望相机之前拍摄的图像存储在设备中,而来自媒体的图像作为两个不同的实体,就像在安卓手机中一样。这可能吗?
    • 你只能使用按画廊挑选,如果你想像 android 你需要使用 filemanager techotopia.com/index.php/Working_with_Files_in_Swift_on_iOS_8
    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 2011-10-14
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    相关资源
    最近更新 更多