Open Image Gallery and Take photo in Swift
Posted By : Nitin Bhatt | 18-Jun-2015
In this blog I have demonstrated how to take a photo from your mobile gallery or with your mobile camera and apply it on an ImageView using swift.
UIImagePickerController
UIImagePickerController is a view controller responsible for allowing the developer to access the user's photo library, camera, saved photos album.
UIImagePickerControllerSourceType have 3 options
- UIImagePickerControllerSourceType.PhotoLibrary
- UIImagePickerControllerSourceType.Camera
- UIImagePickerControllerSourceType.SavedPhotosAlbum
For more Detail about UIImagePickerController go to this link:-
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerController_Class/
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate {
var picker:UIImagePickerController?=UIImagePickerController()
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
picker?.delegate=self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// Open Gallery button click
@IBAction func OpenGallery(sender: AnyObject) {
openGallary()
}
// Take Photo button click
@IBAction func TakePhoto(sender: AnyObject) {
openCamera()
}
func openGallary()
{
picker!.allowsEditing = false
picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
presentViewController(picker!, animated: true, completion: nil)
}
func openCamera()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
picker!.allowsEditing = false
picker!.sourceType = UIImagePickerControllerSourceType.Camera
picker!.cameraCaptureMode = .Photo
presentViewController(picker!, animated: true, completion: nil)
}else{
let alert = UIAlertController(title: "Camera Not Found", message: "This device has no Camera", preferredStyle: .Alert)
let ok = UIAlertAction(title: "OK", style:.Default, handler: nil)
alert.addAction(ok)
presentViewController(alert, animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
var chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.contentMode = .ScaleAspectFit
imageView.image = chosenImage
dismissViewControllerAnimated(true, completion: nil)
}
}
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Nitin Bhatt
Nitin is an Assistant Project Manager specializing in iOS Application Development. He is an avid reader.