iOS Swift - Video picker from library/gallery - ios

I'm trying to implement a feature to pick videos from the gallery from an iPhone.
Here is my function :
func openVideoGallery()
{
let picker = UIImagePickerController()
picker.delegate = self as UIImagePickerControllerDelegate & UINavigationControllerDelegate
picker.sourceType = .savedPhotosAlbum
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .savedPhotosAlbum)!
picker.mediaTypes = ["public.movie"]
picker.allowsEditing = false
present(picker, animated: true, completion: nil)
}
Here is my questions :
I was wondering is it the right way to pick up videos from the gallery using UIImagePickerController ?
How can I get the path to video selected ?
Thank you for your help ^^

1- Yes
2- implemet
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
asset.getURL { (tempPath) in
}
}
else if let media = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
}
else
if let ref = info[UIImagePickerController.InfoKey.referenceURL] as? URL {
}
}
Change this
picker.delegate = self as UIImagePickerControllerDelegate & UINavigationControllerDelegate
to
picker.delegate = self
and make the vc conform to
class VCName: UIViewController,UIImagePickerControllerDelegate & UINavigationControllerDelegate {

Related

Why does my UIImageView replace the second one?

I have two outlets for two different UIImageViews, when I select the first one it'll appear on the first Image View but when I select the second Image, it replaces the first Image View even though it's connected to the second ImageView. This is my code for the Select Image button.
#IBOutlet weak var myImageView1: UIImageView!
#IBOutlet weak var myImageView2: UIImageView!
#IBAction func pickImage1(_ sender: Any) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
self.present(image, animated: true)
}
//Add didFinishPickingMediaWithInfo here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImageView1.image = image
}
else {
//error
}
self.dismiss(animated: true, completion: nil)
}
#IBAction func pickImage2(_ sender: Any) {
let image2 = UIImagePickerController()
image2.delegate = self
image2.sourceType = UIImagePickerControllerSourceType.photoLibrary
image2.allowsEditing = false
self.present(image2, animated: true)
}
//Add didFinishPickingMediaWithInfo here
func imagePickerController2(_ picker2: UIImagePickerController, didFinishPickingMediaWithInfo2 info2: [String : Any]) {
if let image2 = info2[UIImagePickerControllerOriginalImage] as? UIImage {
myImageView2.image = image2
}
else {
//error
}
self.dismiss(animated: true, completion: nil)
}
Try this code. So you need a flag to remember which image view is clicked, and then set image base on that.
var selected = 1
#IBAction func pickImage1(_ sender: Any) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
selected = 1
self.present(image, animated: true)
}
//Add didFinishPickingMediaWithInfo here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
if selected == 1 {
myImageView1.image = image
} else {
myImageView2.image = image
}
}
else {
//error
}
self.dismiss(animated: true, completion: nil)
}
#IBAction func pickImage2(_ sender: Any) {
let image2 = UIImagePickerController()
image2.delegate = self
image2.sourceType = UIImagePickerControllerSourceType.photoLibrary
image2.allowsEditing = false
selected = 2
self.present(image2, animated: true)
}
Moving forward, when you have multiple image views, you can use another method to avoid copying code everywhere.
First, add an unique tag for each image view. Avoid using 0 because the default tag is 0. So you will have image views with tags say 1 to 4.
Call this same method to all your image views so that this function is trigger by clicking on any of them
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.addGestureRecognizer(tapGestureRecognizer)
Handler looks like this
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
let tappedImage = tapGestureRecognizer.view as! UIImageView
selected = tappedImage.tag
self.present(image, animated: true)
}
Finally in image pick delegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
if let imageView = self.view.viewWithTag(selected) as? UIImageView {
imageView.image = image
}
}
else {
//error
}
self.dismiss(animated: true, completion: nil)
}
The problem is that you have renamed the delegate method. If you do that, the method won't be recognized or called.
Another option to the selected answer is to extend UIImageView and have it adhere to UIImagePickerControllerDelegate / UINavigationControllerDelegate.
extension UIImageView: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
//handle error
return
}
image = selectedImage
picker.presentingViewController?.dismiss(animated: true)
}
func presentImagePicker(from viewController: UIViewController) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .photoLibrary
picker.allowsEditing = false
viewController.present(picker, animated: true)
}
}
This is nice because you can then launch the image picker for any UIImageView in your app with one line, like so:
#IBAction func pickImage1(_ sender: UIButton) {
myImageView1.presentImagePicker(from: self)
}

Why "picker.delegate = self necessary"?

I am creating a simple camera app.
In the code, UIImagePickerControllerclass is the delegate of ViewControllerClass instance.
But why "picker.delegate = self"is necessary in this code?
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBAction func launchCamera(_ sender: UIBarButtonItem) {
let camera = UIImagePickerControllerSourceType.camera
if UIImagePickerController.isSourceTypeAvailable(camera) {
let picker = UIImagePickerController()
picker.sourceType = camera
picker.delegate = self
self.present(picker, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imageView.image = image
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
self.dismiss(animated: true)
}
delegate of UIImagePickerController includes UIImagePickerControllerDelegate and UINavigationControllerDelegate.
UIImagePickerControllerDelegate for catch users' action about take photo or choose picture.
UINavigationControllerDelegate for view controller navigation between album and photo

Why doesn't UIImagePickerController delegate to my Object class?

class CameraPicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
weak var viewController:MyProfileVC!
func launchCamera() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker:UIImagePickerController = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
imagePicker.cameraCaptureMode = .photo
imagePicker.allowsEditing = false
self.viewController.present(imagePicker, animated: true, completion: nil)
} }
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("didFinishPickingMedia")
}
This is my object class function, but 'didFinishPickingMediaWithInfo' function doesn't get called after taking the picture. Also, the viewcontroller which is presenting the imagepicker is a different Swift file
I had the same problem and I've found a solution, so I'm posting my version (I'm taking a picture from the photo library but it's the same :) ).
I had a memory management issue.
I've created an IBAction function where I instantiated my camera handler class (with the delegate inside...). At the end of the function the variable goes out of scope and it's deallocated. To solve the issue I've made it as instance variable.
That's my code for the VC with my UiButton:
class STECreateUserVC: UIViewController {
#IBOutlet weak var imgAvatar: UIImageView!
let cameraHandler = STECameraHandler()
#IBAction func buttonPressed(_ sender: UIButton) {
cameraHandler.importPictureIn(self) { [weak self] (image) in
self?.imgAvatar.image = image
}
}
}
...and that's my handler:
class STECameraHandler: NSObject {
let imagePickerController = UIImagePickerController()
var completitionClosure: ((UIImage) -> Void)?
func importPictureIn(_ viewController: UIViewController, completitionHandler:((UIImage) -> Void)?) {
completitionClosure = completitionHandler
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
imagePickerController.sourceType = .photoLibrary
imagePickerController.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
viewController.present(imagePickerController, animated: true, completion: nil)
}
}
extension STECameraHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let completitionClosure = completitionClosure, let image = info[UIImagePickerControllerEditedImage] as? UIImage {
completitionClosure(image)
}
imagePickerController.dismiss(animated: true)
}
}
I've used a closure in order to have a cleaner code.
This:
self.viewController.present(imagePicker, animated: true, completion: nil)
should be:
self.present(imagePicker, animated: true, completion: nil)

UIImageView not updating with new image after picture taken or picture chosen from photo library

I'm trying to populate an image within a UIImageView after I either take a picture or choose a picture from my photo library.
here is the code handling the camera and photo library activations:
#IBAction func activateCmera(_ sender: Any) {
if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
#IBAction func activatePhotoLib(_ sender: Any) {
if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
Here is the code that populates the image within the UIImageView:
#IBOutlet weak var imageView: UIImageView!
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imageView.contentMode = .scaleAspectFit
self.imageView.image = image
self.dismiss(animated: true, completion: nil);
}
currently after I take a picture with my camera or choose an image from my photo library, I'm redirected to the initial view of the app without UIImageView space updated with the photo.
Any help is appreciated thanks!
In Swift 3 you need permission to access photoLibrary by adding below keys to your plist and you need to use the proper delegate method.
Add UIImagePickerControllerDelegate& UINavigationControllerDelegate to your classand set imagePicker delegate to viewDidLoad & Try below code.
let imagePicker = UIImagePickerController()
#IBAction func activatePhotoLib(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.image = image
}
picker.dismiss(animated: true, completion: nil);
}
Output: updated
This seems to be a problem related to XCode failing to explain that the imagePickerController function syntax has changed (since Swift 3). You will find these two ways of writing the function all over the web. You will also be able to use both in XCode without getting any errors. But only the second one will actually get the image properly.
(Option 1 - doesn't work, but XCode doesn't flag it):
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { }
(Option 2 - this is the right syntax!):
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { }
**You need to save the UIImagePickerController like Global Object, after that, you need push this code on your action function
**
#objc func getImage() {
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = false
imagePicker.modalPresentationStyle = .overCurrentContext
present(imagePicker, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.image = image
}
self.dismiss(animated: true, completion: nil);
}
I was having the same issue. Apparently the imagePickerController function has changed again since the last answer. I had to change the function a bit to work with Swift 5.4:
/ Doesn't work
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// Does work
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
The info parameter now expects a dictionary of UIImagePickerController InfoKeys.

Select video from PhotoLibrary in iOS using Swift

I am trying to select video from PhotoLibrary using following code
imageController.mediaTypes = [kUTTypeMovie: NSString]
After selecting video, when it is compressing, I am getting array out of boundary issue.
I searched online and found following line. I am unable to convert it into Swift version (link)
imageController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil]
Please let us know the Swift version for above line. It is very useful if anyone can provide Swift version of this.
Question2:
I want to show selected video in app and need to play when it is tapped. Can we do it without external plugins and using built in libraries?
Based on Swift 2.2
Suppose you have a imagePickerController and when you want to select both images and videos:
let imagePickerController = UIImagePickerController()
var videoURL: NSURL?
#IBAction func selectImageFromPhotoLibrary(sender: UIBarButtonItem) {
imagePickerController.sourceType = .PhotoLibrary
imagePickerController.delegate = self
imagePickerController.mediaTypes = ["public.image", "public.movie"]
presentViewController(imagePickerController, animated: true, completion: nil)
}
Then after the video is selected, print out its NSURL.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
videoURL = info["UIImagePickerControllerReferenceURL"] as? NSURL
print(videoURL)
imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}
For question 2:
Yes, you can do it through AVPlayer, you need import AVKit and AVFoundation, and you code may look like this:
if let videoURL = videoURL{
let player = AVPlayer(URL: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
I made a demo here you can refer, maybe not 100% what you want.
#IBOutlet weak var imgView: UIImageView!
var imagePickerController = UIImagePickerController()
var videoURL : NSURL?
#IBAction func btnSelectVideo_Action(_ sender: Any) {
imagePickerController.sourceType = .savedPhotosAlbum
imagePickerController.delegate = self
imagePickerController.mediaTypes = [kUTTypeMovie as String]
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
videoURL = info[UIImagePickerControllerMediaURL]as? NSURL
print(videoURL!)
do {
let asset = AVURLAsset(url: videoURL as! URL , options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
imgView.image = thumbnail
} catch let error {
print("*** Error generating thumbnail: \(error.localizedDescription)")
}
self.dismiss(animated: true, completion: nil)
}
For question 2:
let player = AVPlayer(url: videoURL)
let playerController = AVPlayerViewController()
playerController.player = player
self.present(playerController, animated: true) {
player.play()
}
Swift 5+ solution:
func openVideoGallery() {
picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .savedPhotosAlbum
picker.mediaTypes = ["public.movie"]
picker.allowsEditing = false
present(picker, animated: true, completion: nil)
}
Swift 3
import MobileCoreServices
var imagePickerController = UIImagePickerController()
var videoURL: URL?
private func openImgPicker() {
imagePickerController.sourceType = .savedPhotosAlbum
imagePickerController.delegate = self
imagePickerController.mediaTypes = ["public.movie"]
present(imagePickerController, animated: true, completion: nil)
}
extension YourViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
videoURL = info[UIImagePickerControllerMediaURL] as? URL
print("videoURL:\(String(describing: videoURL))")
self.dismiss(animated: true, completion: nil)
}
}
func openCamera() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
println("captureVideoPressed and camera available.")
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
imagePicker.mediaTypes = [kUTTypeMovie!]
imagePicker.allowsEditing = false
imagePicker.showsCameraControls = true
self.presentViewController(imagePicker, animated: true, completion: nil)
} else {
println("Camera not available.")
}
}
func imagePickerController( didFinishPickingMediaWithInfo info:NSDictionary!) {
videoUrl = info[UIImagePickerControllerMediaURL] as! NSURL!
let pathString = videoUrl.relativePath
self.dismissViewControllerAnimated(true, completion: nil)
}
Swift 4.0
Your class adopts the UIImagePickerControllerDelegate protocol
class classname: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
After that you need to create
let imagePickerController = UIImagePickerController()
Add this in your button's action:-
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
imagePickerController.mediaTypes = ["public.image", "public.movie"]
present(imagePickerController, animated: true, completion: nil)
Delegate method
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let videoURL = info["UIImagePickerControllerReferenceURL"] as? NSURL
print(videoURL!)
imagePickerController.dismiss(animated: true, completion: nil)
}
Select a Video from Gallery using Swift 4
// Put this code where you want to pick the video from gallery
Let imagePickerController = UIImagePickerController ()
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
imagePickerController.mediaTypes = ["public.movie"]
present(imagePickerController, animated: true, completion: nil)
// UIImagePickerController Delegate Method
func imagePickerController (_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any])
{
let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL
print(videoURL!)
self.dismiss(animated: true, completion: nil)
}
try this pod that i've made :
https://github.com/jhonyourangel/ImagePickerWhatsApp
I was searching for a similar Image and Video Picker for a project and i wasn't able to find one that was able to :
1. Take picture
2. Get a Picture from the library
3. Register a Video
4. Get a video from the library
While i was looking i hopen whatsup of facebook or mabe google have a pod for this kind of needs, i realy liked the whatsapp picker, so i created this one similer to the one in whatsapp.
Prerequisites
In order to use ImageVideoPicker you need cocoapods and an xCode project already created
Installing
to install cocoapods is should be enough just to run sudo gem install cocoapods in the terminal
if this doen't work go to cocoapods
Then navigate the terminal to your project folder, where the file extension .xcodeproj is located and run pod init
this will create a file called Podfile
Open the file in a editor ( sublimeText, xCode, atom, vim ... etc ) and add the line bellow after use_frameworks!
pod 'ImagePickerWhatsApp'
or
pod 'ImagePickerWhatsApp', :path => '/Users/aiu/Documents/cocoapods/ImagePickerWhatsApp'
then just run pod install in the terminal and wait to finish instaling the lib
How to use it
add import ImagePickerWhatsApp to your viewcontroller class
then you can call the picker by calling :
let mp = ImageVideoPicker.makeVCFromStoryboard()
self.present(mp, animated: true, completion: nil)
Delegate
to implement the delegate add mp.delegate = self and the extent the class of you view controller
aso you need to import the iOS Photos framework import Photos
extension ViewController: ImageVideoPickerDelegate {
func onCancel() {
print("no picture selected")
}
func onDoneSelection(assets: [PHAsset]) {
print("selected \(assets.count) assets")
}
}
the func onDoneSelection returns an array of assets that contain the info of where the asset is located : on the device, iTunes library or iCloud.
if you just need to display the images you can use the this code in a collection view or something similar just implement this peace of code
var representedAssetIdentifier: String!
func getImageFrom(asset: Phasset) {
representedAssetIdentifier = asset?.localIdentifier
let imageManager = PHCachingImageManager()
imageManager.requestImage(for: asset!, targetSize: self.frame.size, contentMode: .default, options: nil) { (image, _) in
if(self.representedAssetIdentifier == self.asset?.localIdentifier &&
image != nil) {
self.imageView.image = image
}
}
}
this will show just the thumbnail, but is awesome because is also showing the thumbnail for live photos, and videos
Images and Videos as Data
The lib is intended to be used for sending images or videos over the network, and not to do fancy image or video editing. But this doesn't mea you can't. You can do just about anything since it returns an array of assets, but is you job to implement what you need.
in order to get the data from the asset ImageVideoPicker has one method that will return an completition handler with the data.
ImageVideoPicker.getDataFrom(asset: asset) { (data) in
if data == nil {
print(data as Any, asset.mediaType, asset.localIdentifier)
} else {
print(data!.count as Any, asset.mediaType, asset.localIdentifier)
}
}
that will be all
have fun
func startMediaBrowserFromViewController(viewController: UIViewController!, usingDelegate delegate : protocol<UINavigationControllerDelegate, UIImagePickerControllerDelegate>!) -> Bool {
if UIImagePickerController.isSourceTypeAvailable(.SavedPhotosAlbum) == false {
return false
}
let mediaUI = UIImagePickerController()
mediaUI.sourceType = .SavedPhotosAlbum
mediaUI.mediaTypes = [kUTTypeMovie as String]
mediaUI.allowsEditing = true
mediaUI.delegate = delegate
presentViewController(mediaUI, animated: true, completion: nil)
return true
}
Updated for the current version of Swift:
// Check if the asset is of video media type.
guard (asset.mediaType == PHAssetMediaType.video) else {
print("Not a valid video media type")
return
}
// Obtain the URL of the video.
PHCachingImageManager().requestAVAsset(forVideo: asset, options: nil, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) in
let asset = asset as! AVURLAsset
print("asset.url: ", asset.url) // Here is video URL
// Play the video.
DispatchQueue.main.async(execute: {
let player = AVPlayer(url: asset.url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
})
})
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
println("Camera Available")
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
Delegate method
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
self.dismissViewControllerAnimated(true, completion: nil)
imageView.image = image
}

Resources