从相机显示照片时,UIScrollView不起作用,但是从库中显示

时间:2015-12-27 04:03:47

标签: ios iphone uiscrollview uikit

我对UIScrollView上的有趣行为有疑问。这是相关的代码。

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方法。一切都按我的预期进行。当用户从相机或库中选择照片时,图像视图会附加到contViewcurrentHeight会增加。除了一个有趣的行为。当用户从相机拍摄照片时,我已经冻结了滚动视图。但是,当用户从库中选择图片时,滚动视图将起作用。有没有人知道为什么相机中的照片会导致UIScrollView停止工作而图书馆中的一张照片不会造成任何问题的差异?

0 个答案:

没有答案