我正在编写一个代码,允许绘制并将其保存到照片库中,但是当我点击“共享”按钮时,我总是会收到此错误:
Terminating app due to uncaught exception 'NSGenericException', reason:
'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7ff278e533e0>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
我已阅读其他问题,但我不明白如何修复它,这是我的代码:
Import UIKit
class SignatureController: UIViewController {
@IBOutlet weak var mainImageView: UIImageView!
@IBOutlet weak var tempImageView: UIImageView!
var lastPoint = CGPoint.zero
var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var brushWidth: CGFloat = 10.0
var opacity: CGFloat = 1.0
var swiped = false
let colors: [(CGFloat, CGFloat, CGFloat)] = [
(0, 0, 0),
(105.0 / 255.0, 105.0 / 255.0, 105.0 / 255.0),
(1.0, 0, 0),
(0, 0, 1.0),
(51.0 / 255.0, 204.0 / 255.0, 1.0),
(102.0 / 255.0, 204.0 / 255.0, 0),
(102.0 / 255.0, 1.0, 0),
(160.0 / 255.0, 82.0 / 255.0, 45.0 / 255.0),
(1.0, 102.0 / 255.0, 0),
(1.0, 1.0, 0),
(1.0, 1.0, 1.0),
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Actions
@IBAction func reset(sender: AnyObject) {
mainImageView.image = nil
}
@IBAction func share(sender: AnyObject) {
UIGraphicsBeginImageContext(mainImageView.bounds.size)
mainImageView.image?.drawInRect(CGRect(x: 0, y: 0,
width: mainImageView.frame.size.width, height: mainImageView.frame.size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let activity = UIActivityViewController(activityItems: [image], applicationActivities: nil)
presentViewController(activity, animated: true, completion: nil)
}
@IBAction func pencilPressed(sender: AnyObject) {
var index = sender.tag ?? 0
if index < 0 || index >= colors.count {
index = 0
}
(red, green, blue) = colors[index]
if index == colors.count - 1 {
opacity = 1.0
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
swiped = false
if let touch = touches.first as? UITouch! {
lastPoint = touch.locationInView(self.view)
}
}
func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
// 1
UIGraphicsBeginImageContext(view.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
// 2
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
CGContextAddLineToPoint(context, toPoint.x, toPoint.y)
// 3
CGContextSetLineCap(context, CGLineCap.Round)
CGContextSetLineWidth(context, brushWidth)
CGContextSetRGBStrokeColor(context, red, green, blue, 1.0)
CGContextSetBlendMode(context, CGBlendMode.Normal)
// 4
CGContextStrokePath(context)
// 5
tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
tempImageView.alpha = opacity
UIGraphicsEndImageContext()
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
// 6
swiped = true
if let touch = touches.first as? UITouch! {
let currentPoint = touch.locationInView(view)
drawLineFrom(lastPoint, toPoint: currentPoint)
// 7
lastPoint = currentPoint
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if !swiped {
// draw a single point
drawLineFrom(lastPoint, toPoint: lastPoint)
}
// Merge tempImageView into mainImageView
UIGraphicsBeginImageContext(mainImageView.frame.size)
mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: CGBlendMode.Normal, alpha: 1.0)
tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: CGBlendMode.Normal, alpha: opacity)
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
tempImageView.image = nil
}
}
真的感谢您的帮助
答案 0 :(得分:0)
您是否正在使用iPad编译代码? 我问,因为这是iPad中活动视图控制器的常见错误
在iPad上,活动视图控制器将使用新的UIPopoverPresentationController显示为弹出窗口,因此,您应该在代码中解决它
如何正确设置示例:
if ( [activityViewController respondsToSelector:@selector(popoverPresentationController)] ) {
activityViewController.popoverPresentationController.sourceView = parentView
}
我希望这有助于你
答案 1 :(得分:0)
UIPopoverPresentationController(&lt; _UIAlertControllerActionSheetRegularPresentationController:0x7ff278e533e0&gt;)应该在演示文稿发生之前设置一个非零的sourceView或barButtonItem .-表示您没有设置应该呈现UIImagePickerController的源视图。您还可以设置应从中显示弹出框的矩形。下面的代码位于视图控制器中。
试试此代码
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = false
let actionsheet:UIAlertController = UIAlertController(title: "", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let popper = imagePicker.popoverPresentationController
popper?.delegate = self
popper?.sourceView = self.view
popper?.sourceRect = CGRectMake(self.view.bounds.width / 2.0 , self.view.bounds.height / 2.0, 1.0, 1.0)
actionsheet.popoverPresentationController?.sourceView = self.view
actionsheet.popoverPresentationController?.sourceRect = CGRectMake(self.view.bounds.width / 2.0 , self.view.bounds.height / 2.0, 1.0, 1.0)
self.presentViewController(actionsheet, animated: true, completion: nil)