如何为UIImagePicker(SWIFT)更改/创建新的源类型

时间:2015-01-30 21:36:16

标签: ios xcode swift uikit uiimagepickercontroller

我是iOS开发的新手,我需要帮助。 我使用UIImagePicker,以便用户选择他的图像。 我希望该用户选择位于应用程序文件夹而不是iOS库中的图片。所有这些都使用UIImagePicker。这可能吗? 如果我错了,请将我重定向到网站或教程。

谢谢大家。

示例代码:

类ViewController:UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate {

@IBOutlet var PhotoOpeningView: UIImageView!

let userDefaults = NSUserDefaults.standardUserDefaults()


override func viewDidLoad() {
    super.viewDidLoad()



    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



@IBAction func ChooseAImageBTN(sender: AnyObject) {

    let imagePickerView = UIImagePickerController()
    imagePickerView.delegate = self

    imagePickerView.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
    self.presentViewController(imagePickerView, animated: true, completion: nil)

}


@IBAction func btnSave(sender: UIButton) {
    let LoadSaveNo = PhotoOpeningView
    userDefaults.setObject(LoadSaveNo, forKey: "LoadSave")
    userDefaults.synchronize()
}


func imagePickerController(picker :UIImagePickerController,
    didFinishPickingMediaWithInfo info : [NSObject : AnyObject]) {
        var Image = info[UIImagePickerControllerOriginalImage] as UIImage
        PhotoOpeningView.image = Image
        self.dismissViewControllerAnimated(true, completion: nil)

}

2 个答案:

答案 0 :(得分:0)

浏览the UIImagePickerController documentation,我不认为你问的是可能的。如果你看一下UIImagePickerControllerSourceType,你会看到只有3种可能性,PhotoLibrary,SavedPhotosAlbum或者Camera,它们没有为尝试“自定义”的源类型留下足够的空间。

我的建议是使用UICollectionView显示图像,并允许用户使用集合视图委托方法从中进行选择。如果你需要的只是一个简单的图像选择器,你可以很快地鞭打一个。

目标-C: http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

夫特: http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-1

答案 1 :(得分:0)

//This class has UIButton when you click on button it opens PhotoPickersVC. Which will show all images from your application
class ViewController: UIViewController,PhotoPickersDelegate {

    @IBOutlet var imageView:UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    @IBAction func buttonBrowsePhotos(sender: UIButton)
    {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let nc :UINavigationController = storyboard.instantiateViewControllerWithIdentifier("PhotoNC") as UINavigationController
        var array:NSArray = nc.viewControllers as NSArray
        let vc:PhotoPickersVC=array.objectAtIndex(0) as PhotoPickersVC;
        vc.delegate=self;
        self.presentViewController(nc, animated: true, completion: nil)

    }

    func selectPhoto(image: UIImage)
    {
        imageView.image=image;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}



//Protocol used to send photo to ViewController class using delegate
protocol PhotoPickersDelegate
{
    func selectPhoto(var image : UIImage)
}

//This class shows all the photos of your application
class PhotoPickersVC: UICollectionViewController {
    let reuseIdentifier = "Cell"
    var myImages :NSArray?
    var delegate : PhotoPickersDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        var myBundle : NSBundle = NSBundle.mainBundle();
        myImages = myBundle.pathsForResourcesOfType("jpeg", inDirectory: nil);
    }

    @IBAction func buttonCancel(sender: UIButton)
    {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //#warning Incomplete method implementation -- Return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return myImages!.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as PhotoCellCollectionViewCell
        print(cell);




        var imageName=myImages?.objectAtIndex(indexPath.row).lastPathComponent
        var imageName1 :NSString=imageName!
        var image:UIImage=UIImage(named: imageName1)!
        cell.imageView.image=image

        return cell
    }

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
    {

        if((self.delegate) != nil)
        {
            var imageName=myImages?.objectAtIndex(indexPath.row).lastPathComponent
            var imageName1 :NSString=imageName!
            var image:UIImage=UIImage(named: imageName1)!

            delegate?.selectPhoto(image)
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }


}



//This class used to subclass UICollectionViewCell which is used in PhotoPickersVC
class PhotoCellCollectionViewCell: UICollectionViewCell {
    @IBOutlet var imageView: UIImageView!
}