【发布时间】:2016-07-19 18:24:37
【问题描述】:
我正在尝试编写代码以使相机仅从我在 iPad 上的应用程序拍摄方形照片。我在这里找到了一个从Objective-C翻译成Swift的解决方案,但是在我用代码拍照后,“使用照片”和“重拍”按钮不会响应触摸,除非我将iPad旋转90度并且从横向模式点击按钮。这当然是非常不可取的。任何关于如何修复我的代码的提示都会很棒。
@IBAction func takePicture(sender: UIButton)
{
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let takePictureAction = UIAlertAction(title: "Take Picture", style: UIAlertActionStyle.Default)
{ (alertAction: UIAlertAction!) -> Void in
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
var f: CGRect
f = picker.view.bounds
//f.size.width -= picker.navigationBar.bounds.size.width
f.size.height -= picker.navigationBar.bounds.size.height
var barHeight: CGFloat
barHeight = (f.size.height - f.size.width) / 2
UIGraphicsBeginImageContext(f.size)
var edgeColor: UIColor
edgeColor = UIColor(white: 0, alpha: 0.5)
edgeColor.set()
UIRectFillUsingBlendMode(CGRectMake(0, 0, f.size.width, barHeight), CGBlendMode.Normal)
UIRectFillUsingBlendMode(CGRectMake(0, f.size.height - barHeight, f.size.width, barHeight), CGBlendMode.Normal)
var overlayImage: UIImage
overlayImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let overlayIV = UIImageView(frame: f)
overlayIV.image = overlayImage
picker.cameraOverlayView?.addSubview(overlayIV)
self.presentViewController(picker, animated: true, completion: nil)
}
let chooseExistingPicture = UIAlertAction(title: "Use Existing", style: UIAlertActionStyle.Default) {(alertAction: UIAlertAction!) -> Void in
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
self.presentViewController(picker, animated: true, completion: nil)
}
alertController.addAction(takePictureAction)
alertController.addAction(chooseExistingPicture)
alertController.view.tintColor = UIColor.blackColor()
presentViewController(alertController, animated: true, completion: nil)
let presentationController = alertController.popoverPresentationController
presentationController?.sourceView = self.view
presentationController?.sourceRect = CGRectMake(330, 210, 330, 210)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
var image = info[UIImagePickerControllerOriginalImage]
let imageSize = image?.size
let width = Double((imageSize?.width)!)
let height = Double((imageSize?.height)!)
if width != height {
let newDimension = min(width, height)
let widthOffset = (width - newDimension) / 2
let heightOffset = (height - newDimension) / 2
UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGFloat(newDimension), CGFloat(newDimension)), false, 0.0)
image?.drawAtPoint(CGPointMake(CGFloat(-widthOffset), CGFloat(-heightOffset)))
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
CustomPicture.image = image as? UIImage;
dismissViewControllerAnimated(true, completion: nil)
}
【问题讨论】:
标签: ios swift uiimagepickercontroller