gpt4 book ai didi

swift - 使用 UIImagePickerController 加载相机或 PhotoLibrary?

转载 作者:行者123 更新时间:2023-11-30 12:59:31 24 4
gpt4 key购买 nike

我最近在 iOS 10 上的 Swift 3 中输入了拍照或选择图片的代码。当调用打开的相机或照片库时,我的应用程序崩溃并显示“线程 14:信号 SIGABRT”,且没有任何消息。正在发生的事情的其他迹象。对我来说,根据我的调试,这种情况是在尝试加载 native 相机 View 或照片库时发生的。任何帮助将不胜感激!

代码如下:

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

<key>NSPhotoLibraryUsageDescription</key>
<string>Use Photos</string>

enter image description here

检查这个link了解更多详情

关于swift - 使用 UIImagePickerController 加载相机或 PhotoLibrary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40011536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com