This example is to show how to pick an image from a soure ( in this case, the Saved Photos )
First create the function for the Image Picker:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { var x = CGFloat(arc4random_uniform(100)+50) var y = CGFloat(arc4random_uniform(100)+50) var image = info[UIImagePickerControllerOriginalImage] as UIImage // create a variable from the image var imageViewNew = UIImageView(frame: CGRectMake(100, 100, 150 , 150)) // creating a new UIImageView for the image once loaded on the screen imageViewNew.center = CGPoint(x: x, y: y) imageViewNew.image = image imageView.addSubview(imageViewNew) // add the newly created UIImageView to the main Imageview Subview imageViewNew.multipleTouchEnabled = true imageViewNew.userInteractionEnabled = true // UIImageView by default has these disabled createPanGestureRecognizer(imageViewNew) createPinchGestureRecognizer(imageViewNew) // adding gesture to the newly created image ? Keep in mind you have to create these functions separately. createRotateGestureRecognizer(imageViewNew) dismissViewControllerAnimated(true, completion: nil) } |
Then this is what we are actually gonna tie up to our button for loading an image:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
@IBAction func imageButtonPressed(sender: AnyObject) { var imagePicker = UIImagePickerController() var sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum if UIImagePickerController.isSourceTypeAvailable(sourceType) { imagePicker.sourceType = sourceType imagePicker.delegate = self // show a screen presentViewController(imagePicker, animated: true, completion: nil) } } |
Hi!
How would I do this so it loads from a UICollectionView cell when selected?
I’m trying to make an App where the user can load an image from the photoAlbum, and then load a image from the app as an overlay.
I have the UIImagePickerController all set and working with subViews and gestureRecognizers, I have UICollectionView setup and populated with my overlay images. But how do i do it so that when i select a cell it loads the image into the same imageView as the subViews?
Any advice would be much appreciated!
I already answered on your similar question, but to be honest you can always use a barbaric way of doing it, if you only have a small number of UIViews. Let’s say you have 4 by 3 UIVIews = 12 total. Create a variable that would be like : var cellNumber: Int = 1 . Then you can change it depending on which UIView you touch and have the image placed in the correct subView taken from that ‘var’… That is probably the worst way of doing it 🙂 I might look into that UICollectionView soon, it does seem like something useful. I found another article on someone having a similar problem btw: http://stackoverflow.com/questions/17516715/uicollectionview-doesnt-show-cell-images