2016-12-17 59 views
0
class CameraPicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
weak var viewController:MyProfileVC! 

func launchCamera() { 
     if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) { 
      let imagePicker:UIImagePickerController = UIImagePickerController() 
      imagePicker.delegate = self 
      imagePicker.sourceType = UIImagePickerControllerSourceType.camera 
      imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front 
      imagePicker.cameraCaptureMode = .photo 
      imagePicker.allowsEditing = false 

      self.viewController.present(imagePicker, animated: true, completion: nil) 
     } } 
} 
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     print("didFinishPickingMedia") 
} 

這是我的對象類函數,但'didFinishPickingMediaWithInfo'函數在拍攝照片後不會被調用。此外,這是呈現imagepicker的視圖 - 控制是一個不同的斯威夫特文件爲什麼不把UIImagePickerController委託給我的Object類?

+0

你在模擬器或手機上試過這個嗎?它不會在模擬器上工作,因爲模擬器沒有相機 –

+0

我在我的手機上試過這個 – Dee

+0

@Dee你在info.plist中添加了'NSPhotoLibraryUsageDescription' ? –

回答

-1

此:

self.viewController.present(imagePicker, animated: true, completion: nil) 

應該是:

self.present(imagePicker, animated: true, completion: nil) 
+0

該類沒有viewcontroller。我創建了類來處理所有的UIImagePicker函數,所以我創建了一個對viewcontroller的弱引用,該引用使用這個類來管理我如何呈現imagePicker – Dee

+0

你是如何將實際視圖控制器的引用傳遞給'weak var viewController: MyProfileVC!'? –

+0

對不起,延遲迴復。在CameraPicker的初始化方法中,我爲發件人添加了一個參數,將其引入弱變量 – Dee

0

我有同樣的問題,我已經找到了解決辦法,所以我發佈我的版本(我正在拍攝照片庫中的照片,但它是一樣的:))。

我有內存管理問題。 我創建了一個IBAction函數,我實例化了我的相機處理程序類(其中的代表...)。在函數結束時,變量超出範圍,並且它被釋放。爲了解決這個問題,我將它作爲實例變量。

這是我的代碼爲我的UIButton的VC:

class STECreateUserVC: UIViewController { 

    @IBOutlet weak var imgAvatar: UIImageView! 
    let cameraHandler = STECameraHandler() 

    @IBAction func buttonPressed(_ sender: UIButton) { 
     cameraHandler.importPictureIn(self) { [weak self] (image) in 
      self?.imgAvatar.image = image 
     } 
    } 
} 

...這就是我的處理程序:

class STECameraHandler: NSObject { 

    let imagePickerController = UIImagePickerController() 
    var completitionClosure: ((UIImage) -> Void)? 

    func importPictureIn(_ viewController: UIViewController, completitionHandler:((UIImage) -> Void)?) { 
     completitionClosure = completitionHandler 
     imagePickerController.delegate = self 
     imagePickerController.allowsEditing = true 
     imagePickerController.sourceType = .photoLibrary 
     imagePickerController.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)! 
     viewController.present(imagePickerController, animated: true, completion: nil) 
    } 
} 

extension STECameraHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     if let completitionClosure = completitionClosure, let image = info[UIImagePickerControllerEditedImage] as? UIImage { 
      completitionClosure(image) 
     } 
     imagePickerController.dismiss(animated: true) 
    } 
} 

我纔能有一個更乾淨的代碼使用的封閉。

相關問題