2015-04-20 257 views
1

該視圖只加載一個圖像,並允許用戶放大和縮小以近距離查看圖像的某些部分。但是,它會以巨大的尺寸加載,我希望它以屏幕尺寸加載。或者如果屏幕尺寸很難得到,那麼只需加載50%的尺寸就可以。UIScroll視圖設置初始圖像尺寸小於全尺寸

override func viewDidLoad() { 
    super.viewDidLoad() 

// Do any additional setup after loading the view. 

    self.scrollView.minimumZoomScale = 0 
    self.scrollView.maximumZoomScale = 1 
    self.scrollView.delegate = self 
    self.imageView.image = self.image! 
    } 

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? { 
    return self.imageView 
    } 
} 

如何將初始圖像尺寸設置爲合理尺寸,但允許縮放爲全尺寸圖像?

謝謝!

+0

scrollView.zoomScale = view.frame.width/imageView.image.size.width –

+0

添加'scrollView.zoomScale = view.frame.width /內viewDidLoad中imageView.image.size.width'沒有按」 t在加載時似乎對圖像的大小有任何影響。 @LeonardoSavioDabus –

+0

您是否嘗試添加下面的圖像imageView.image = image! ? –

回答

0

您只需計算將您的視圖寬度除以圖像大小寬度的變焦作爲f ollow:

scrollView.zoomScale = view.frame.width/imageView.image.size.width 
0

獲取屏幕尺寸:(目標-C)

let screenSize: CGRect = UIScreen.mainScreen().bounds 
let screenWidth = screenSize.width 
let screenHeight = screenSize.height 

設置的UIImageView幀到屏幕大小,然後設定縱橫以適應:

imageView.contentMode = .ScaleAspectFit (UIViewContentModeScaleAspectFit in Obj-C) 

然後,只需設置zoomScale:

self.scrollView.zoomScale = .5; 
+0

我是新手,很抱歉,如果我不理解。我在viewDidLoad中添加了以下代碼,並且在加載時對圖像的大小沒有影響。 '讓屏幕尺寸:的CGRect = UIScreen.mainScreen()邊界 讓屏幕寬度= screenSize.width 讓screenHeight = screenSize.height imageView.contentMode = .ScaleAspectFit self.scrollView.zoomScale = 0.5;' –