我希望用户从他的照片库或相机中选择一张照片。我找不到任何例子。我想用 UIAlertView
之类的东西提示用户。
我的代码适用于照片库。
@IBAction func selectLeftPhoto(sender: AnyObject) {
flag = 1
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(myPickerController, animated: true, completion: nil)
}
@IBAction func selectRightButton(sender: AnyObject) {
flag = 2
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
if flag == 1 {
leftImage.image = pickedImage
let imageData = UIImageJPEGRepresentation(pickedImage!, 0.5)
let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
photo1 = base64String
}else if flag == 2 {
rightImage.image = pickedImage
let imageData = UIImageJPEGRepresentation(pickedImage!, 0.5)
let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
photo2 = base64String
}
dismissViewControllerAnimated(true, completion: nil)
}
我的用户界面:
我想在点击选择照片按钮后提示用户从照片库或相机中进行选择。
let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default)
{
UIAlertAction in
self.openCamera()
}
let gallaryAction = UIAlertAction(title: "Gallary", style: UIAlertActionStyle.Default)
{
UIAlertAction in
self.openGallary()
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
{
UIAlertAction in
}
// Add the actions
picker?.delegate = self
alert.addAction(cameraAction)
alert.addAction(gallaryAction)
alert.addAction(cancelAction)
// Present the controller
if UIDevice.currentDevice().userInterfaceIdiom == .Phone
{
self.presentViewController(alert, animated: true, completion: nil)
}
else
{
popover=UIPopoverController(contentViewController: alert)
popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
关于ios - 要求用户使用 swift2 从照片库或相机中选择照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36835286/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |