UIImagePicker无法正常运行

时间:2018-07-01 18:42:57

标签: swift firebase uiimagepickercontroller

我正在尝试允许用户更改其个人资料图片并将其保存到Firebase。我构建了代码,我的应用程序开始运行。但是,我没有看到更改个人资料图片的功能。我的代码如下:

import { map, reduce, isEmpty } from 'rxjs/operators';
import { Observable } from 'rxjs';

  list$: Observable<IOrder[]>;
  total$: Observable<number>;
  empty$: Observable<boolean>;


ngOnInit() {
    this.list$ = this.service.asList.asObservable(); // asList is a BehaviorSubject
    this.empty$ = this.list$.pipe(isEmpty());
    this.total$ = this.list$.pipe(
      map(order => order.price),  // <-- Property 'price' does not exist on type 'IOrder[]'
      reduce((total, price) => total + price, 0)
    );
}

我还创建了一个用于处理imagePicker的扩展文件:

import UIKit
import Firebase

class EditProfileVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(profileImageView)
        setupProfileImageView()

        self.view.backgroundColor = UIColor.white
    }

    func setupProfileImageView() {
        profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        profileImageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive=true
        profileImageView.widthAnchor.constraint(equalToConstant: 150).isActive = true
        profileImageView.heightAnchor.constraint(equalToConstant: 150).isActive = true
    }

    let profileImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "users")
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFill

        imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
}

是否存在无法正确插入的内容或缺少关键代码?

2 个答案:

答案 0 :(得分:1)

从代码开头删除此代码块,因为它是重复的,带有1个大写字母,并且我认为您正在分配要重复的图像。

let ProfileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "users")
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()

答案 1 :(得分:1)

我在项目中使用了类似的东西。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    var selectedImageFromPicker: UIImage?

    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
        selectedImageFromPicker = editedImage

    } else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
        selectedImageFromPicker = originalImage

    }
    if let selectedImage = selectedImageFromPicker {
        userImg = selectedImage

    }
    userImageView.image = userImg
    dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}