【发布时间】:2016-07-03 04:50:17
【问题描述】:
我制作了一个用户拍照的 iPhone 应用。我需要在我的代码中添加什么,以便用户在拍照时将照片保存到用户库中?
以下是我目前的代码
导入 UIKit 导入 AVFoundation
类视图控制器:UIViewController、UIImagePickerControllerDelegate、UINavigationControllerDelegate {
var picker = UIImagePickerController()
@IBOutlet var camera: UIButton!
@IBAction func camera(sender: UIButton) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker = UIImagePickerController() //make a clean controller
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
picker.showsCameraControls = true
//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"
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)
}
//MARK: Picker Delegates
func imagePickerController(
picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil)
}
//What to do if the image picker cancels.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true,
completion: nil)
}
}
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.
}
}
【问题讨论】:
标签: swift uiimagepickercontroller