gpt4 book ai didi

ios - 我如何将静态 ImageView 更改为用户选择的图像

转载 作者:行者123 更新时间:2023-11-28 08:42:56 26 4
gpt4 key购买 nike

我是整个 iOS 开发的新手。我想问一下,我有一个带有 ImageView 和按钮的 vc(文本字段和标签不是问题),我希望用户点击选择或拍摄个人资料照片时。用户将拍摄或选择的照片,它将取代我的静态 ImageView 。我怎样才能做到这一点? 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)
}

}

最佳答案

在使用 UIAlertController 时,我通常这样做:

1。设置警报

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)

不要忘记将代表设置为在之后接收选择或拍摄的照片。这几乎就是布莱克所说的。

2。拍照

在委托(delegate)函数中,我获取图像并用它做任何我想做的事。例如,您可以将它用于 UIImageView

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {     
self.imageView.image = image
self.dismissViewControllerAnimated(true)
}

差不多就这些了。<​​/p>

关于ios - 我如何将静态 ImageView 更改为用户选择的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com