There is a UIImageView in which the aspectToFit mode is installed. How to get the size of the fit image view size?
1 answer
It is necessary to calculate it from the sizes of UIImage and UIImageView :
@IBOutlet var ivToGetSize: UIImageView! //UIImageView Ρ ΠΈΡΠΊΠΎΠΌΠΎΠΉ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΎΠΉ //ΠΏΠΎ Π½Π°ΠΆΠ°ΡΠΈΡ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅ΠΌ ΡΠ°Π·ΠΌΠ΅Ρ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ @IBAction func btnClick(sender: UIButton) { let image = ivToGetSize.image if let im = image { let displayedSize = getDisplayedSize(im.size, imageViewBoundsSize: ivToGetSize.bounds.size) let imWidth = displayedSize.width let imHeight = displayedSize.height print("\(imWidth) \(imHeight)") //Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΠ΅ ΡΠΈΡΠΈΠ½Ρ ΠΈ Π²ΡΡΠΎΡΡ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ } } //ΡΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ ΡΠ°Π·ΠΌΠ΅ΡΠ° func getDisplayedSize(imageSize: CGSize, imageViewBoundsSize: CGSize) -> CGSize { var imageDisplayedSize = CGSizeMake(imageViewBoundsSize.width, imageViewBoundsSize.height); let mW = imageViewBoundsSize.width / imageSize.width; let mH = imageViewBoundsSize.height / imageSize.height; if( mH < mW ) { imageDisplayedSize.width = mH * imageSize.width; } else if( mW < mH ) { imageDisplayedSize.height = mW * imageSize.height; } return imageDisplayedSize; } |