Swift在viewdidappear中显示图像无法通过选择新图像来替换

时间:2016-07-18 20:16:55

标签: swift

我使用firebase作为我的后端,我在viewdidappear中显示了facebook照片图像功能,因此当用户在viewcontroller时将显示配置文件图像。我的问题是每当用户想要更改配置文件时,图像选择器将显示"选择的图像"仅限1秒,然后转回Facebook图片。

我认为问题是显示的facebook照片图片功能是在viewdidappear中吗?我该如何解决?

override func viewdidappear(){
  super.viewdidappear(animated)
  self.displayProfilePic(user)
}

func displayProfilePic(user: FIRUser?){

let photoURL = user?.photoURL
struct last {
  static var photoURL: NSURL? = nil
} 
last.photoURL = photoURL;  later one.
if let photoURL = photoURL {
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
    let data = NSData.init(contentsOfURL: photoURL)
    if let data = data {
      let image = UIImage.init(data: data)
      dispatch_async(dispatch_get_main_queue(), {
        if (photoURL == last.photoURL) {
          self.profilePic.image = image

        }
      })
    }

  })
} else {
  profilePic.image = UIImage.init(named: "DefaultPic")
}

图片选择器方法

  func openGallary(){

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
  print("Pick Photo")

  self.imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
  self.imagePicker.allowsEditing = true
  self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
}

func openCamera(){
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
  self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
  self.imagePicker.allowsEditing = true
  self.presentViewController(self.imagePicker, animated: true, completion: nil)
}else{
  print("you got no camara")
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMeddiaWithInfo info: [String :AnyObject]){
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

  profilePic.contentMode = .ScaleAspectFit
  profilePic.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}

我不确定为什么图像选择器的拾取图像无法替换。

1 个答案:

答案 0 :(得分:0)

  

我认为问题是显示的facebook照片图片功能是在viewdidappear中显示的

这是对的。每次在覆盖它的VC被取消后再次显示视图时(例如图像选择器),都会调用viewDidAppear

你可以在viewDidLoad中执行此操作 - 在加载VC时只调用一次 - 并且在完成imagePicker时不再调用。

如果你不能,那么你需要跟踪imagePicker是否替换了图像,并且在调用viewDidAppear时不应该再次使用FB图像替换它。