2016-06-18 3 views
0

내 코드를 오버라이드 (override) 할 수 없습니다스위프트 3 - 'imagePickerController'

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { 
    self.image = image 
    self.imageStatusLabel.text = "There is a picture" 
    self.imageStatusLabel.textColor = UIColor.blue() 

    picker.dismiss(animated: true, completion: nil) 
} 

을 그리고 이것은 오류입니다 :

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
    // But no image in the parameters 
} 
:

Cannot override 'imagePickerController' which has been marked unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift

글쎄, 나는 새로운 기능을 사용하려고했습니다

하지만 매개 변수에 이미지가 없으므로 이미지를 얻을 수 있습니다.
어떻게해야합니까?

답변

1

당신은 전달 info 사전에서 이미지를 얻을 당신은 정보 매개 변수를 사용해야하는 documentation

당신은 아마 원하는

중 하나

let image = info[UIImagePickerControllerOriginalImage] as? UIImage 

또는

let image = info[UIImagePickerControllerEditedImage] as? UIImage 
11

의 키를 참조 UIImage으로 캐스트하세요. info.plist 파일에 "Privacy - Photo Library Usage Description"과 "Privacy - Camera Usage Description"을 넣고 값을 사진 라이브러리 및 카메라에 대한 액세스 요청 이유를 설명하는 문자열로 채워야한다는 것을 잊지 마십시오.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     pictureImageView.contentMode = .scaleToFill 
     pictureImageView.image = pickedImage 
    } 

    picker.dismiss(animated: true, completion: nil) 
} 
2

당신이 키를 사용하여 행을 추가해야합니다 Swift3에서 imagePickerController를 사용하려면 : Privacy - Photo Library Usage Description을 가치가있는 string (예 : "사진-액세스") 의 Info.plist 파일과의 아래 해보자 :

class YourController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    let imagePickerButton: UIButton = { 
     let btn = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100)) 
     btn.setTitle("Choose a Photo", for: .normal) 
     btn.setTitleColor(.white, for: .normal) 
     btn.backgroundColor = .blue 
     btn.addTarget(self, action: #selector(handlePickerDidTap), for: .touchUpInside) 

     return btn 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     view.addSubview(imagePickerButton) 
    } 

    func handlePickerDidTap() { 
     let imagePickerController = UIImagePickerController() 
     imagePickerController.delegate = self 
     imagePickerController.allowsEditing = true 
     present(imagePickerController, animated: true, completion: nil) 
    } 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     print("photo is selected") 
     // do some thing here 
    } 

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

} 

희망!

0
import UIKit 
import Photos 

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

@IBOutlet weak var imageView: UIImageView! 

@IBAction func selectPhotoLibrary(_ sender: Any) { 
    let picker = UIImagePickerController() 
    picker.delegate = self 
    if UIImagePickerController.isSourceTypeAvailable(.camera) { 
     picker.sourceType = .camera 
    } else{ 
     print("Kamera desteklenmiyor.") 
    } 
    present(picker, animated: true, completion:nil) 
} 

@IBAction func selectPhotoCamera(_ sender: Any) { 
    let picker = UIImagePickerController() 
    picker.delegate = self 
    picker.sourceType = .photoLibrary 
    present(picker, animated: true, completion:nil) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    picker.dismiss(animated: true, completion:nil) 
    guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else { return } 
    guard let imageData = UIImageJPEGRepresentation(image, 0.8) else { return } 
    self.imageView.image = image 
    print(imageData) 
} 

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

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
    } 
} 

사용 세부 사항에 대해 자세히 알아보기 ->https://medium.com/@yakupad/swift-uiimagepickercontroller-kullanımı-91771ed4c1d6