不明白为什么我没有更新我的类属性(imageData属性)。
简单的例子,2个视图控制器嵌入导航控制器(对我来说很重要):
第一个:带有imageView的ViewController和第二个视图控制器的按钮(通过segue)
class ViewController: UIViewController, choosePicVCDelegate {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.clipsToBounds = true
}
func goodPic(vc: choosePicViewController) {
if let data = vc.imageData {
let image = UIImage(data: data)
self.imageView.image = image
}
vc.navigationController?.popViewControllerAnimated(true)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let vc = segue.destinationViewController as! choosePicViewController
vc.delegate = self
}
}
第二个:choosePicViewController与imagePicker一起选择要在第一个ViewController中显示的图像。 2个按钮:一个用于选择图像,另一个用于返回带有委托的第一个视图控制器..
protocol choosePicVCDelegate {
func goodPic(vc: choosePicViewController)
}
class choosePicViewController: UIViewController {
var delegate: choosePicVCDelegate?
let imagePicker = UIImagePickerController()
var imageData: NSData?
@IBAction func back(sender: UIButton) {
delegate?.goodPic(self)
}
@IBAction func coverPick(sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
}
extension choosePicViewController: UIImagePickerControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.imageData = UIImageJPEGRepresentation(image, 1.0)
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
总是从imageData属性获得nil,不明白为什么。
答案 0 :(得分:0)
我认为问题是你的扩展,我不明白为什么在这种情况下使用扩展,无论如何。试试吧。
您需要从imagePicker.delegate = self
设置委托,因为这样您可以监听UIImagePickerController
委托。我相信你决心。
protocol choosePicVCDelegate {
func goodPic(vc: choosePicViewController)
}
class choosePicViewController: UIViewController, UIImagePickerControllerDelegate {
var delegate: choosePicVCDelegate?
let imagePicker = UIImagePickerController()
var imageData: NSData?
override func viewWillAppear(animated: Bool) {
imagePicker.delegate = self;
}
override func viewDidDisappear(animated: Bool) {
imagePicker.delegate = nil;
}
@IBAction func back(sender: UIButton) {
delegate?.goodPic(self)
}
@IBAction func coverPick(sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
//Delegates from UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let img = info[UIImagePickerControllerOriginalImage] as? UIImage{
self.imageData = UIImageJPEGRepresentation(img, 1.0)
delegate?.goodPic(self)
dismissViewControllerAnimated(true, completion: nil)
}
}
}
希望能帮到你!