我是整个iOS开发的新手。我想问一下,我有一个带有图像视图和按钮的vc(文本字段和标签不是问题),我想在用户点击选择或拍摄个人资料照片时使用。用户将要拍摄或选择的照片,它将替换我的静态图像视图。我怎样才能做到这一点?这是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)
}
}
当使用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)
}
就是这样。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句