无法在滚动视图中缩放UIImage

时间:2015-09-18 12:33:55

标签: ios swift uiscrollview uiimage scrollview

我试图让用户在像;

这样的结构中缩放我的图像

的ViewController - >滚动视图 -----> UIImage的

我可以滚动页面,但无法放大或缩小。我想我在这里错过了一些东西。谢谢....

这是我的代码:

import UIKit
import Parse
import ParseUI

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var image: UIImageView!

    @IBOutlet weak var label: UILabel!

    @IBOutlet weak var scrollView: UIScrollView!

    var currentObject : PFObject?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.scrollView.maximumZoomScale = 5.0
        self.scrollView.minimumZoomScale = 0.5
        self.scrollView.delegate = self
        self.scrollView.addSubview(image)

        func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
            return image
        }
            if let object = currentObject {

            if let imageName = object["file"] as? PFFile {
                imageName.getDataInBackgroundWithBlock({
                    (result, error) in
                    self.image.image = UIImage(data: result!)
                })
            }
            label.text = object["fileName"] as? String
        }
            }

        override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

1 个答案:

答案 0 :(得分:4)

只需将viewForZoomingInScrollView方法移出viewDidLoad

您的代码将是:

import UIKit
import Parse
import ParseUI

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var image: UIImageView!

    @IBOutlet weak var label: UILabel!

    @IBOutlet weak var scrollView: UIScrollView!

    var currentObject : PFObject?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.scrollView.maximumZoomScale = 5.0
        self.scrollView.minimumZoomScale = 0.5
        self.scrollView.delegate = self
        self.scrollView.addSubview(image)

        if let object = currentObject {

            if let imageName = object["file"] as? PFFile {
                imageName.getDataInBackgroundWithBlock({
                    (result, error) in
                    self.image.image = UIImage(data: result!)
                })
            }
            label.text = object["fileName"] as? String
        }
    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return image
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

希望它有所帮助。