2015-07-10 68 views
0

我使用這個代碼來接從畫廊的圖像,並適應它裏面UIScrollView無法適應的UIImage裏面的UIScrollView

let imageView = UIImageView(image: inputImageDelegate!) 
imageView.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height) 
imageView.contentMode = UIViewContentMode.ScaleAspectFill 
inputImage = imageView 
contentView.addSubview(imageView) 

self.scrollView.minimumZoomScale = 0.5 
self.scrollView.maximumZoomScale = 6.0 
self.scrollView.contentSize = self.inputImage.frame.size 

self.scrollView.delegate = self 
self.scrollView.zoomScale = 1 
imageView.sizeToFit() 

結果: enter image description here

我想使圖像的寬度等於加載後的屏幕寬度(用戶可以將其縮放)。 self.scrollView.zoomScale = 1imageView.sizeToFit()都沒有工作。

回答

0

我假設你有你的滾動視圖連接到IBOutlet這樣的:

@IBOutlet weak var scrollView: UIScrollView! 

應設置UIImageView的基礎上,UIScrollViewcontentSize財產frame

let imageView = UIImageView(image: inputImageDelegate!) 
let contentSize = self.scrollView.contentSize // convenience 
imageView.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height) 
...