我正在尝试使用this tutorial.创建项目我尽力尝试重新创建代码,但遇到了一些问题。这是我的代码:
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var ImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func choosePhoto(sender: AnyObject) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "Choose image source", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera roll", style: UIAlertActionStyle.Default, handler: { ( alert: UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(imagePickerController, animated: true, completion: nil)
} ))
actionSheet.addAction(UIAlertAction(title: "Take Photo", style: UIAlertActionStyle.Default, handler: { ( alert: UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(imagePickerController, animated: true, completion: nil)
} ))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(actionSheet, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image: UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
ImageView.image = image
picker.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func applyFilter(sender: AnyObject) {
applyFilterWithName("CIPhotoEffectMono")
}
func applyFilterWithName(filterName: String){
let originlOrientation = ImageView.image!.imageOrientation
let originalScale = ImageView.image!.scale
let inputImage = CIImage(image : ImageView.image)
let drawingContext = CIContext(options: nil)
let filterParameters = [kCIInputImageKey: inputImage]
let filter = CIFilter(name: filterName, withInputParameters: filterParameters)
let outputImage = filter.outputImage
let imageRef = drawingContext.createCGImage(outputImage, fromRect: outputImage.extent())
ImageView.image = UIImage(CGImage: imageRef, scale: originalScale, orientation: originlOrientation)
}
@IBAction func saveImage(sender: AnyObject) {
UIImageWriteToSavedPhotosAlbum(ImageView.image, nil, nil, nil)
}
我知道这是我必须看的调试器输出的主要部分:
Image Filter[20320:1554357] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type 1 not available'
我似乎无法弄清楚这个错误在哪里或如何修复它。再一次,我尽我所能重新创建代码,但可能会有错误。
非常感谢任何建议或意见。
提前致谢。
答案 0 :(得分:2)
你的代码毫无意义。你在说:
actionSheet.addAction(UIAlertAction(title: "Camera roll", style: UIAlertActionStyle.Default, handler: { ( alert: UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(imagePickerController, animated: true, completion: nil)
} ))
如果用户要点击一个"相机胶卷"按钮,为什么你会创建一个Camera
源类型的图像选择器控制器?这不是用户要求的内容。
顺便说一句,错误意味着没有相机。您不是试图在模拟器中运行此代码,是吗?这也是毫无意义的。相机仅存在于设备上 - 设备带有相机。