Working in Swift 3. I found numerous questions with answers and then also blogs, yet everything I've tried didn't work. I am just trying to capture a camera shot I take and save it to the documents. But it isn't being saved as it doesn't show up under devices documents when viewed from within xcode and I don't get any errors or similar. I'm at a bit of a lost here.
Code that have for getting the image and saving it
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var pickedImage = UIImage()
pickedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
picker.dismiss(animated: true, completion: nil)
let currentDateTime = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMddHHmmss"
let fileNameWithExtension = "ts_\(formatter.string(from: currentDateTime)).png"
//create path
let imagePath = fileInDocumentsDirectory(filename: fileNameWithExtension)
imageStringPathSet = fileNameWithExtension
imageSet = pickedImage
if saveImage(image: pickedImage, path: imagePath) {
cameraButton.setImage(#imageLiteral(resourceName: "ic_camerashot_yes60dp"), for: UIControlState.normal)
return
}
cameraButton.setImage(#imageLiteral(resourceName: "ic_camerashot_no60dp"), for: UIControlState.normal)
}
func fileInDocumentsDirectory(filename: String)-> URL {
return try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent(filename)
}
func saveImage(image: UIImage, path: URL) -> Bool {
print(path)
guard let pngImageData = UIImagePNGRepresentation(image) else {
print("error")
return false
}
var resultValid = false
do {
let results = try pngImageData.write(to: path, options: [.atomicWrite])
print(results) //prints ()
resultValid = true
}
catch {
resultValid = false
print(error)
}
return resultValid
}
When I print the path it prints
file:///var/mobile/Containers/Data/Application/01EB6A70-34C6-4481-BE5B-7F7AB5E6703F/Documents/ts_20161221145652.png
Which I believe is correct. If everything works correctly, it changes an image on the screen yet it never changes and the imageStringPathSet isn't set either which is a class variable. Anyone have any ideas on what I need to do to get this to work?
Solution
Turns out the cause was that I was resetting everything in the view in viewWillAppear. Once I fixed this, things worked fine. Thanks everyone for your feedback. Hope this helps someone else to not do what I did.
As per the request in the comments, here's my code that (a) uses the UIImagePickerController to either select from the camera roll, then (b) uses the UIActivityViewController to let the user choose among several possible ways to save/attach the image to several sources of output.
While this isn't saving to the document directory, it may be a better route to go. Please pay attention to a few notes after the code listing.
My "select" view controller, which allows a user to either pick from the camera roll or take a picture:
extension SelectViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Camera App
func openCameraApp() {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,
animated: true,
completion: nil)
} else {
noCamera()
}
}
func noCamera(){
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .alert)
let okAction = UIAlertAction(
title: "OK",
style:.default,
handler: nil)
alertVC.addAction(okAction)
present(
alertVC,
animated: true,
completion: nil)
}
// MARK: Photos Albums
func showImagePicker() {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
// picker.modalPresentationStyle = .Popover
present(picker,
animated: true,
completion: nil)
picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
image = chosenImage
self.performSegue(withIdentifier: "ShowEditView", sender: self)
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: false, completion: nil)
}
// MARK: Seque to EditViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowEditView" {
if let vc = segue.destination as? EditViewController {
vc.image = image
}
}
}
}
My "edit" view controller, which is embedded in a UINavigationBar with a button on it set to System Item == Action:
// MARK: Image actions
#IBAction func shareImage(_ sender: UIBarButtonItem) {
let context = CIContext()
let final = context.createCGImage(imgEdited, from: imgEdited.extent)
let shareImage = UIImage(cgImage: final!)
let vc = UIActivityViewController(activityItems: [shareImage], applicationActivities: [])
vc.excludedActivityTypes = [
//UIActivityTypePostToTwitter,
//UIActivityTypePostToFacebook,
UIActivityType.postToWeibo,
//UIActivityTypeMessage,
//UIActivityTypeMail,
UIActivityType.print,
//UIActivityTypeCopyToPasteboard,
UIActivityType.assignToContact,
//UIActivityTypeSaveToCameraRoll,
UIActivityType.addToReadingList,
//UIActivityTypePostToFlickr,
UIActivityType.postToVimeo,
UIActivityType.postToTencentWeibo
]
present(vc,
animated: true,
completion: nil)
vc.popoverPresentationController?.sourceView = self.view
vc.completionWithItemsHandler = {(activity, success, items, error) in
}
}
Notes:
Keep in mind that when using the UIImagePickerController in an iOS 10 device, it will crash unless you add this to your info.plist:
<key>NSCameraUsageDescription</key>
<string>Used to capture new image for photo effect</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Used to select an image for photo effect</string>
You may use whatever you wish in the tag.
The UIActivityViewController will present all commented out options in it's excludedActivityTypes. (I keep these options listed just for self documentation.) If a user has Facebook and wants to post to it, they'll be prompted to login if they aren't yet.
Related
I am having an issue while displaying camera captured image in an image view where delegates are set and being called "didFinishPickingMediaWithInfo".
I have two buttons for the user interaction (camera picture & from gallery), once I take picture from camera and then select any other image from gallery; the camera picture taken earlier is shown for a second. I tried searching a lot but no luck so far.
Can anyone please advise what am I missing. Following is the code for reference.
#IBAction func cameraButtonPressed(_ sender: UIButton) {
self.openCamera()
}
#IBAction func galleryButtonPressed(_ sender: UIButton) {
self.openGallary()
}
override func viewDidLoad() {
super.viewDidLoad()
initialLayout()
}
override func viewWillAppear(_ animated: Bool) {
self.lblPreview.isHidden = true
self.imageTake.isHidden = true
self.uploadButtonOutlet.isHidden = true
}
//
//MARK: - Internal Methods
func initialLayout() {
self.cameraButtonOutlet.layer.cornerRadius = 20
self.galleryButtonOutlet.layer.cornerRadius = 20
self.uploadButtonOutlet.layer.cornerRadius = 20
}
//MARK: - Open the camera
func openCamera(){
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)){
imagePicker.sourceType = UIImagePickerController.SourceType.camera
//If you dont want to edit the photo then you can set allowsEditing to false
imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.cameraCaptureMode = .photo
imagePicker.cameraDevice = .rear
self.present(imagePicker, animated: true, completion: nil)
}
else{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
//MARK: - Choose image from camera roll
func openGallary(){
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
imagePicker.allowsEditing = true
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
}
extension UploadTimesheetViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
self.lblPreview.isHidden = false
self.imageTake.isHidden = false
self.uploadButtonOutlet.isHidden = false
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
self.imageTake.image = editedImage
self.imageTake.setNeedsLayout()
})
// self.imageTake.image = editedImage
}
//Dismiss the UIImagePicker after selection
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.isNavigationBarHidden = false
self.dismiss(animated: true, completion: nil)
}
}
Update your delegate method like this,
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var finalImage:UIImage?
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
finalImage = image
}else {
finalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
}
self.lblPreview.isHidden = false
self.imageTake.isHidden = false
self.uploadButtonOutlet.isHidden = false
self.imageTake.image = finalImage
//Dismiss the UIImagePicker after selection
picker.dismiss(animated: true, completion: nil)
}
Sorry for the delayed response. Just updating the answer so if anyone is having the same issue should know what was the actual problem.
Once picker is dismissed a viewwillappear will be called where I have hid the image view and hence the reason it was not displaying it for the first time.
If I remove the code from viewwillappear and add that properties code to viewedload then all works fine.
The issue was once the picker is dismissed , viewillapear was called and that was creating the issue.
I am trying to have a user choose an image from their gallery or camera, then upload it to the app. This works, but the only problem is that it doesn't save it in the app. As soon as the user closes the app, the image that the user chose disappears. I also do not have any save function because i don't know how to implement one.
I am using Xcode 8.3.2 in Swift 3.0. Here is the code below:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func chooseImage(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
}else{
print("Camera not available")
}
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = image
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Remember to save image you need to store image as NSData
// Code to store image
let defaults = UserDefaults.standard
// To save as data:
// From StoryBoard, if you want to save "image" data on the imageView of
// MainStoryBoard, following codes will work.
let image = UIImagePNGRepresentation(imageView.image!) as NSData?
defaults.set(image, forKey: "test") // saving image into userdefault
// for retrieving the image
if (UserDefaults.standard.object(forKey: "test") as? NSData) != nil {
let photo = UserDefaults.standard.object(forKey: "test") as! NSData
img2.image = UIImage(data: photo as Data) // img2 set your imageview on which you want photo to appear
// Now you can set img2.image
}
Edited
How to use in your code
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) {
let defaults = UserDefaults.standard
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = image
let saveImage = UIImagePNGRepresentation(image!) as NSData?
defaults.set(saveImage, forKey: "test") // saving image into userdefault
picker.dismiss(animated: true, completion: nil)
}
And in your view did load use retrieving method
If the image exist and in nsdata format then only it will show save image. Thats it.
You can save image in your document directory with below function
func saveImageDocumentDirectory(tempImage:UIImage){
let documentsDirectoryURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = documentsDirectoryURL.appendingPathComponent("ImageName.png")
do {
try UIImagePNGRepresentation(tempImage)?.write(to: fileURL)
} catch {
print(error)
}
}
To retrieve Image you can use
func getImage()->URL?{
let documentsDirectoryURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = documentsDirectoryURL.appendingPathComponent("ImageName.png")
if FileManager.default.fileExists(atPath: fileURL.path){
return fileURL
}else{
return nil
}
}
You can use any name you like and store image with different name to store multiple image.
This is the code:
// Only allow photos to be picked,not taken.
imagePickerController.sourceType = .photoLibrary
this is the error: Type 'UIImagePickerControllerSourceType' has no member 'photoLibrary'
Just trying to complete the official tutorial Start Developing iOS Apps (Swift)
//MARK: Actions
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
//Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagepickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked,not taken.
imagePickerController.sourceType = .photoLibrary
//Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
present(imagePickerController, animated: true, competion: nil)
}
#IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
Okay, it looks like you are probably usingSwift 2.2 (maybe it's this for Swift 2.3 also). You want this syntax:
imagePickerController.sourceType = .PhotoLibrary
Note the capitalization. Keep in mind, Xcode 8.2 (released 12 December 2016) will be the last version of Xcode to support Swift 2.x. Sometime in 2017 you will probably need to use Swift 3 for new App Store submissions.
EDIT:
Here's my full Swift 2.2 code, followed by my Swift 3.0 code. Please note two things:
I'm also using the Camera app and have checks to make sure it's present - which isn't the case for the simulator.
Your app will crash in iOS 10 unless you add the following to your info.plist file:
NSCameraUsageDescription
Used to capture new image for photo effect
NSPhotoLibraryUsageDescription
Used to select an image for photo effect
You may put different values in the tags.
Swift 2.2:
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
extension SelectViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Camera App
func openCameraApp() {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
picker.modalPresentationStyle = .FullScreen
presentViewController(picker,
animated: true,
completion: nil)
} else {
noCamera()
}
}
func noCamera(){
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .Alert)
let okAction = UIAlertAction(
title: "OK",
style:.Default,
handler: nil)
alertVC.addAction(okAction)
presentViewController(
alertVC,
animated: true,
completion: nil)
}
// MARK: Photos Albums
func showImagePicker() {
picker.allowsEditing = false
picker.sourceType = .PhotoLibrary
// picker.modalPresentationStyle = .Popover
presentViewController(picker,
animated: true,
completion: nil)
picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
image = chosenImage
self.performSegueWithIdentifier("ShowEditView", sender: self)
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(false, completion: nil)
}
// MARK: Seque to EditViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowEditView" {
if let vc = segue.destinationViewController as? EditViewController {
vc.image = image
}
}
}
}
Swift 3.0 (only the extension code has syntax changes):
func openCameraApp() {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,
animated: true,
completion: nil)
} else {
noCamera()
}
}
func noCamera(){
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .alert)
let okAction = UIAlertAction(
title: "OK",
style:.default,
handler: nil)
alertVC.addAction(okAction)
present(
alertVC,
animated: true,
completion: nil)
}
// MARK: Photos Albums
func showImagePicker() {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
// picker.modalPresentationStyle = .Popover
present(picker,
animated: true,
completion: nil)
picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
image = chosenImage
self.performSegue(withIdentifier: "ShowEditView", sender: self)
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: false, completion: nil)
}
// MARK: Seque to EditViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowEditView" {
if let vc = segue.destination as? EditViewController {
vc.image = image
// vc.image = images[0]
}
}
}
I try to write an App that needs a screen where you can take multible photos. I have used a code example from http://makeapppie.com/2015/11/04/how-to-make-xib-based-custom-uiimagepickercontroller-cameras-in-swift/.
It seems to be working OK, but my imagePickerController didFinishPickingMediaWithInfo newer get called. I am getting an error message from Xcode "Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates." It sounds to me like this could be the problem, and I have googled it, but havn't gotten any wiser. A lot of people write it's an Apple bug and I havn't found anybody offering a solution.
So do anybody know if it is the Xcode error that is my problem, and in that case have a solution for that or have I written something wrong in my code:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, CustomOverlayDelegate {
var picker = UIImagePickerController()
#IBAction func shootPhoto(sender: AnyObject) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker = UIImagePickerController() //make a clean controller
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
picker.showsCameraControls = false
//customView stuff
let customViewController = CustomOverlayViewController(
nibName:"CustomOverlayViewController",
bundle: nil
)
let customView:CustomOverlayView = customViewController.view as! CustomOverlayView
customView.frame = self.picker.view.frame
customView.cameraLabel.text = "Hello Cute Camera"
customView.delegate = self
//presentation of the camera
picker.modalPresentationStyle = .FullScreen
presentViewController(picker, animated: true,completion: {
self.picker.cameraOverlayView = customView
})
} else { //no camera found -- alert the user.
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .Alert)
let okAction = UIAlertAction(
title: "OK",
style:.Default,
handler: nil)
alertVC.addAction(okAction)
presentViewController(
alertVC,
animated: true,
completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print("didFinishPickingMediaWithInfo")
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //get the image from info
UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil) //save to the photo library
}
//What to do if the image picker cancels.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
//MARK: Custom View Delegates
func didCancel(overlayView:CustomOverlayView) {
picker.dismissViewControllerAnimated(true, completion: nil)
print("dismissed!!")
}
func didShoot(overlayView:CustomOverlayView) {
picker.takePicture()
overlayView.cameraLabel.text = "Shot Photo"
print("Shot Photo")
}
func weAreDone(overlayView: CustomOverlayView) {
picker.dismissViewControllerAnimated(true,
completion: nil)
print("We are done!")
}
}
Write
picker.delegate = self after
picker = UIImagePickerController() line
Also inherit you class with UIImagePickerControllerDelegate delegate.
It will work.
I would like to be able to view a list of both photos and videos stored on the user's iPhone so I can allow them to select the file for upload. So far, I have it working where photos show up in the list, but no videos are showing up. The code I'm using to display the photos library is the following:
#IBAction func btnAddPicOrVideo(sender: AnyObject) {
let pickerC = UIImagePickerController()
pickerC.delegate = self
self.presentViewController(pickerC, animated: true, completion: nil)
}
As I mentioned, I'm able to display a list of photos and select one of them just fine. The problem is that I'm unable to see or select any videos. Is there a way to specify for both pictures and videos to be displayed? Or, do I have to display pictures and videos separately?
I'm currently running my code on the simulator and I have a video file stored on it locally.
Thanks in advance.
I was able to get this resolved by specifying
import MobileCoreServices
and I changed the code I specified above as such:
#IBAction func btnAddPicOrVideo(sender: AnyObject) {
let pickerC = UIImagePickerController()
pickerC.mediaTypes = [kUTTypeImage as NSString, kUTTypeMovie as NSString]
pickerC.delegate = self
self.presentViewController(pickerC, animated: true, completion: nil)
}
class ScoutDetailPage: UIViewController,UIImagePickerControllerDelegate {
var picker:UIImagePickerController? = UIImagePickerController()
let imageView = UIImageView ()
{
override func viewDidLoad(){
// Do any additional setup after loading the view.
self.loadOrTakePhotos()
}
func loadOrTakePhotos()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
{
picker!.sourceType = UIImagePickerControllerSourceType.Camera
picker?.delegate = self
self .presentViewController(picker!, animated: true, completion: nil)
}
}
else if (pickersegment.selectedSegmentIndex == 1)
{
picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
picker?.delegate = self
self.presentViewController(picker!, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let pickedimage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = pickedimage
if (imageView.image != nil)
{
print("image not empty")
// Do something here.
picker .dismissViewControllerAnimated(false, completion: nil)
}
else
{
print("IMAGE VIEW NIL")
}
}
func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
if error != nil {
let alert = UIAlertController(title: "Save Failed",
message: "Failed to save image",
preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "OK",
style: .Cancel, handler: nil)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true,
completion: nil)
}
}
}