2016-03-04 73 views
0

我試圖使用UIScrollview實現圖像縮放功能。在那裏我保持形象的方面適合。
圖像在UIScrollView之內,並且圖像幀已被給出類似於UIScrollView
這是我的代碼。在UIScrollview上縮放圖像後,它滾動到圖像區域外

class CroppingImageViewController: UIViewController, UIScrollViewDelegate{  
    var image:UIImage = UIImage() 

    @IBOutlet var imageview:UIImageView! 
    @IBOutlet var scrollview:UIScrollView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     imageview.image = image 
     scrollview.contentSize = imageview.frame.size; 
     scrollview.minimumZoomScale = 1.0; 
     scrollview.maximumZoomScale = 4.0; 
     scrollview.setZoomScale(scrollview.minimumZoomScale, animated: true) 
     scrollview.delegate = self; 
    } 

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

What's actually happening
What required
我的要求是,圖像應被向上捲動到它的內容。

回答

0
self.automaticallyAdjustsScrollViewInsets=NO; 

根據apple文檔的說明,automaticAdjustScrollViewInsets默認爲YES。

所以它試圖控制你的scrollview。

將此添加到您的viewdidload方法中,然後嘗試。

+0

它仍然是一樣的。沒有運氣 !! – abhinavroy23

0

我一直在尋找相同的東西。我確實喜歡這樣。它的一種爲我工作。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     let image = info[UIImagePickerControllerOriginalImage] as! UIImage 
     imageView.image = image 

     imageView.contentMode = UIViewContentMode.center 
     imageView.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height) 

     ScrollView.contentSize = imageView.frame.size 

     let scrollViewFrame = ScrollView.frame 
     let scaleWidth = scrollViewFrame.size.width/ScrollView.contentSize.width 
     let scaleHeight = scrollViewFrame.size.height/ScrollView.contentSize.height 

     let minScale = min(scaleHeight, scaleWidth) 

     ScrollView.minimumZoomScale = minScale 
     ScrollView.maximumZoomScale = 1 
     ScrollView.zoomScale = minScale 
     ScrollView.clipsToBounds = true 

     centerScrollViewContents() 

     picker.dismiss(animated: true, completion: nil) 

    } 

func centerScrollViewContents(){ 
    let boundsSize = ScrollView.bounds.size 
     var contentsFrame = imageView.frame 

     if contentsFrame.size.width < boundsSize.width{ 
      contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2 

     }else{ 
      contentsFrame.origin.x = 0 
     } 

     if contentsFrame.size.height < boundsSize.height{ 
      contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2 
     }else{ 
      contentsFrame.origin.y = 0 
     } 

     imageView.frame = contentsFrame 
    }