使用 UIImagePickerController 加载相机或照片库?
Load Camera or PhotoLibrary with UIImagePickerController?
我最近在 iOS 10 上的 Swift 3 中输入了拍照或 select 照片的代码。调用打开相机或照片库时,我的应用程序崩溃并显示 "Thread 14: signal SIGABRT",但没有其他迹象表明发生了什么。对我来说,从我调试过的情况来看,这种情况是在尝试加载本机相机视图或照片库时发生的。任何帮助将不胜感激!
代码如下:
class CameraViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
var imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func BTN_takePhoto(_ sender: AnyObject) {
print("button pressed")
if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){
print("source available")
//imagePicker = UIImagePickerController()
print(1)
imagePicker.delegate = self
print(2)
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
print(3)
imagePicker.mediaTypes = [kUTTypeImage as String]
print(4)
imagePicker.allowsEditing = true
print(5)
self.present(imagePicker, animated: true, completion: nil)
print(6)
}
else {
print("ERROR: source not available")
NSLog("No Camera")
let alert = UIAlertController(title: "No camera", message: "Please allow this app the use of your camera in settings or buy a device that has a camera.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
@IBAction func BTN_pickFromLibrary(_ sender: AnyObject) {
imagePicker.allowsEditing = false //2
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary //3
present(imagePicker, animated: true, completion: nil)//4
}
func imagePickerController(imagePicker: UIImagePickerController, didFinishPickingMediaWithInfo info: NSDictionary!) {
NSLog("Received image from camera")
let mediaType = info[UIImagePickerControllerMediaType] as! String
var originalImage:UIImage?, editedImage:UIImage?, imageToSave:UIImage?
let compResult:CFComparisonResult = CFStringCompare(mediaType as NSString!, kUTTypeImage, CFStringCompareFlags.compareCaseInsensitive)
if ( compResult == CFComparisonResult.compareEqualTo ) {
editedImage = info[UIImagePickerControllerEditedImage] as! UIImage?
originalImage = info[UIImagePickerControllerOriginalImage] as! UIImage?
if ( editedImage != nil ) {
imageToSave = editedImage
} else {
imageToSave = originalImage
}
imageView.image = imageToSave
imageView.reloadInputViews()
}
imagePicker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
imagePicker.dismiss(animated: true, completion: nil)
}
/*
private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imagePicker.dismiss(animated: true, completion: nil)
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
*/
}
从 iOS 10 开始,您现在必须在 Info.plist 中添加密钥
<key>NSPhotoLibraryUsageDescription</key>
<string>Use Photos</string>
查看此link了解更多详情
我最近在 iOS 10 上的 Swift 3 中输入了拍照或 select 照片的代码。调用打开相机或照片库时,我的应用程序崩溃并显示 "Thread 14: signal SIGABRT",但没有其他迹象表明发生了什么。对我来说,从我调试过的情况来看,这种情况是在尝试加载本机相机视图或照片库时发生的。任何帮助将不胜感激!
代码如下:
class CameraViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
var imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func BTN_takePhoto(_ sender: AnyObject) {
print("button pressed")
if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){
print("source available")
//imagePicker = UIImagePickerController()
print(1)
imagePicker.delegate = self
print(2)
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
print(3)
imagePicker.mediaTypes = [kUTTypeImage as String]
print(4)
imagePicker.allowsEditing = true
print(5)
self.present(imagePicker, animated: true, completion: nil)
print(6)
}
else {
print("ERROR: source not available")
NSLog("No Camera")
let alert = UIAlertController(title: "No camera", message: "Please allow this app the use of your camera in settings or buy a device that has a camera.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
@IBAction func BTN_pickFromLibrary(_ sender: AnyObject) {
imagePicker.allowsEditing = false //2
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary //3
present(imagePicker, animated: true, completion: nil)//4
}
func imagePickerController(imagePicker: UIImagePickerController, didFinishPickingMediaWithInfo info: NSDictionary!) {
NSLog("Received image from camera")
let mediaType = info[UIImagePickerControllerMediaType] as! String
var originalImage:UIImage?, editedImage:UIImage?, imageToSave:UIImage?
let compResult:CFComparisonResult = CFStringCompare(mediaType as NSString!, kUTTypeImage, CFStringCompareFlags.compareCaseInsensitive)
if ( compResult == CFComparisonResult.compareEqualTo ) {
editedImage = info[UIImagePickerControllerEditedImage] as! UIImage?
originalImage = info[UIImagePickerControllerOriginalImage] as! UIImage?
if ( editedImage != nil ) {
imageToSave = editedImage
} else {
imageToSave = originalImage
}
imageView.image = imageToSave
imageView.reloadInputViews()
}
imagePicker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
imagePicker.dismiss(animated: true, completion: nil)
}
/*
private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imagePicker.dismiss(animated: true, completion: nil)
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
*/
}
从 iOS 10 开始,您现在必须在 Info.plist 中添加密钥
<key>NSPhotoLibraryUsageDescription</key>
<string>Use Photos</string>
查看此link了解更多详情