ios - 要求用户使用 swift2 从照片库或相机中选择照片
<p><p>我希望用户从他的照片库或相机中选择一张照片。我找不到任何例子。我想用 <code>UIAlertView</code> 之类的东西提示用户。 </p>
<p>我的代码适用于照片库。</p>
<pre><code>@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: )
{
let pickedImage = info 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)
}
</code></pre>
<p>我的用户界面:</p>
<p> <a href="/image/6AAwW.png" rel="noreferrer noopener nofollow"><img src="/image/6AAwW.png" alt="enter image description here"/></a> </p>
<p>我想在点击选择照片按钮后提示用户从照片库或相机中进行选择。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code> 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)
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 要求用户使用 swift2 从照片库或相机中选择照片,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36835286/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36835286/
</a>
</p>
页:
[1]