Ios 从相机显示照片时,UIScrollView不起作用,但从库显示
我在UIScrollView上有一个关于有趣行为的问题。这是相关代码Ios 从相机显示照片时,UIScrollView不起作用,但从库显示,ios,iphone,uiscrollview,uikit,Ios,Iphone,Uiscrollview,Uikit,我在UIScrollView上有一个关于有趣行为的问题。这是相关代码 class TipCreationViewController: UIViewController, MKMapViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextViewDelegate, UITextFieldDelegate { @IBOutlet weak var scrollView: UI
class TipCreationViewController: UIViewController, MKMapViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextViewDelegate, UITextFieldDelegate {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var contView: UIView!
var iView: UIImageView?
var currentHeight: CGFloat = 0 {
didSet {
self.scrollView.contentSize = CGSize(width: self.scrollView.contentSize.width, height: self.currentHeight)
}
}
private var imageAttached: UIImage?
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
self.imagePicker.delegate = self
self.imagePicker.allowsEditing = false
self.currentHeight = 150
}
@IBAction func cameraWasPressed(sender: AnyObject) {
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
let alertViewController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let actionFromLibrary = UIAlertAction(title: "From Library", style: .Default) { (_) -> Void in
self.imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
let actionFromCamera = UIAlertAction(title: "From Camera", style: .Default) { (_) -> Void in
self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
let actionCancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertViewController.addAction(actionFromLibrary)
alertViewController.addAction(actionFromCamera)
if imageAttached != nil {
let actionDeletePhoto = UIAlertAction(title: "Remove Image", style: .Default, handler: { (_) -> Void in
self.deletePhotoIfThere()
})
alertViewController.addAction(actionDeletePhoto)
}
alertViewController.addAction(actionCancel)
alertViewController.view.tintColor = SHREDLIGHTS_BLUE
self.presentViewController(alertViewController, animated: true, completion: nil)
}
//THIS SEEMS TO BE THE FUNCTION IN QUESTION...
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
deletePhotoIfThere()
self.imageAttached = pickedImage
let height = self.view.frame.width * pickedImage.scale
self.iView = UIImageView(frame: CGRectMake(0, self.currentHeight, self.contView.frame.width, height))
self.iView!.contentMode = UIViewContentMode.ScaleAspectFit
self.iView!.image = pickedImage
self.currentHeight += height
self.contView.addSubview(self.iView!)
}
self.dismissViewControllerAnimated(true, completion: nil)
}
func deletePhotoIfThere() {
if let iv = self.iView {
self.currentHeight -= iv.frame.height
iv.removeFromSuperview()
self.iView?.removeFromSuperview()
self.iView = nil
}
}
}
因此,其工作方式是用户点击cameraWasPressed
按钮。这将显示一个操作表,允许用户选择是要拍照还是从库中选择一张。现在,我想我遇到的问题是imagePickerController
方法。一切都如我所料。图像视图附加到contView
,当用户从相机或库中选择照片时,currentHeight
将增加。除了一个有趣的行为。当用户从相机拍摄照片时,我的滚动视图冻结。但是,当用户从库中选择图片时,滚动视图将起作用。有人知道为什么相机的照片会导致UIScrollView
停止工作,而库中的照片不会导致任何问题,这方面存在差异吗