我是整个iOS开发的新手。我想问一下,我有一个带有图像视图的vc和一个按钮(文本字段和标签不是问题),我希望当用户点击选择或拍摄个人资料照片时。用户将拍摄或选择的照片,它将替换我的静态图像视图。我怎么能做到这一点? Here is the vc
这是我的代码:
import UIKit
class CreateAccountViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var profilePic: UIImageView!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var emailTxtField: UITextField!
@IBOutlet weak var pwdTextFld: UITextField!
@IBOutlet weak var retypePwdTextField: UITextField!
@IBOutlet weak var startEarningRewardsBtn: UIButton!
let myPickerController = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
startEarningRewardsBtn.layer.cornerRadius = 4
UIApplication.sharedApplication().statusBarStyle = .Default
myPickerController.delegate = self
let tapGesture = UITapGestureRecognizer(target: self, action:
"imageTapped:")
profilePic.addGestureRecognizer(tapGesture)
profilePic.userInteractionEnabled = true
}
func imageTapped(gesture:UIGestureRecognizer) {
if let _ = gesture.view as? UIImageView {
print("Image Tapped")
showActionSheet()
}
}
func camera() {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func photoLibrary() {
let myPickerController = UIImagePickerController()
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func showActionSheet() {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
self.camera()
}))
actionSheet.addAction(UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
self.photoLibrary()
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(actionSheet, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject!]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
profilePic.contentMode = .ScaleAspectFit
profilePic.image = pickedImage
}
self.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
答案 0 :(得分:1)
使用UIAlertController
时,我通常会这样做:
let alertController = UIAlertController(title: "Change Photo", message: "To change your photo, you can:", preferredStyle: .ActionSheet)
let takePhotoAction = UIAlertAction(title: "Take Photo", style: .Default, handler: {
(alertAction) -> Void in
if UIImagePickerController.isCameraDeviceAvailable(.Front) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
self.presentViewController(imagePicker, animated: true, completion: nil)
}
})
let pickPhotoAction = UIAlertAction(title: "Pick Photo", style: .Default, handler: {
(alertAction) -> Void in
if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}
})
alertController.addAction(takePhotoAction)
alertController.addAction(pickPhotoAction)
self.presentViewController(alertController, animated: true, completion: nil)
不要忘记将委托设置为接收拍摄或拍摄的照片。这几乎是布莱克所说的。
在委托功能中,我拍摄图像并随心所欲地做任何事情。例如,您可以将其用于UIImageView
。
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.imageView.image = image
self.dismissViewControllerAnimated(true)
}
这就是它。
答案 1 :(得分:0)
在photoLibary
和camera
方法中,由于您在myPickerController
内声明并在myPickerController
中声明的myPickerController
,因此声明init
photoLibary
实际上并没有调用{1}}方法。因此,一旦方法camera
和myPickerController
完成,就不再有对func camera() {
myPickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func photoLibrary() {
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(myPickerController, animated: true, completion: nil)
}
的本地实例的引用,因此当您完成选择图像时,没有对代理的引用
self.dismissViewControllerAnimated(true, completion: nil)
同样如前所述,您需要在以下选择器委托方法中更改此项:
picker.dismissViewControllerAnimated(true, completion: nil)
到此:
disconnected
答案 2 :(得分:-1)
你的代码很好。您只需将它放在dispatch_async块中作为更新UI,它需要在UI主队列中运行。
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
dispatch_async(dispatch_get_main_queue(),
{
self.profilePic.contentMode = .ScaleAspectFit
self.profilePic.image = pickedImage
})
}