[英]How to get the displayed image frame from UIImageView?
我修改了 UIImageView 的 contentMode 屬性。 所以我的圖像的框架不等於 UIImageView 的框架。 我的問題是如何從 UIImageView 檢索圖像的框架。
如下例,橙色矩形是 UIImageView 的邊框。 很明顯,圖像的框架與 UIImageView 不一樣。
如果它是UIViewContentModeScaleAspectFit
,它是這樣的:
UIImageView *iv; // your image view
CGSize imageSize = iv.image.size;
CGFloat imageScale = fminf(CGRectGetWidth(iv.bounds)/imageSize.width, CGRectGetHeight(iv.bounds)/imageSize.height);
CGSize scaledImageSize = CGSizeMake(imageSize.width*imageScale, imageSize.height*imageScale);
CGRect imageFrame = CGRectMake(roundf(0.5f*(CGRectGetWidth(iv.bounds)-scaledImageSize.width)), roundf(0.5f*(CGRectGetHeight(iv.bounds)-scaledImageSize.height)), roundf(scaledImageSize.width), roundf(scaledImageSize.height));
事實上,它是從 iOS 4 開始內置的(對於UIViewContentModeScaleAspectFit
):
@import AVFoundation;
AVMakeRectWithAspectRatioInsideRect(imageView.image.size, imageView.bounds);
由於框架組件可能不是整數,請考慮通過以下方式包裝調用
CGRectIntegral(…)
有關更多技巧,請參閱http://nshipster.com/image-resizing/ 。
使用來自 AVFoundation 的這個 api
#import <AVFoundation/AVFoundation.h>
CGRect imageRect =
AVMakeRectWithAspectRatioInsideRect(imageView.image.size, imageView.bounds);
if you want to calculate it yourself, use below code written by me.
func getImageFrameInImageView(imageView : UIImageView) -> CGRect {
/*
by 徐明剛, ericxu1983@163.com
算法:
設高寬比 r = h/w, 則:r(imageView) 縮寫成r(v), r(image)縮寫為r(i),
if r(i) > r(v), 則
h(i) = h(v),
h(i) / w(i) = r(i) -> w(i) = h(i) / r(i)
y = 0
x = (w(v) / 2) - (w(i) / 2)
反之
*/
let image = imageView.image!
let wi = image.size.width
let hi = image.size.height
print("wi:\(wi), hi:\(hi)")
let wv = imageView.frame.width
let hv = imageView.frame.height
print("wv:\(wv), hv:\(hv)")
let ri = hi / wi
let rv = hv / wv
print("ri:\(ri), rv:\(rv)")
var x, y, w, h: CGFloat
if ri > rv {
h = hv
w = h / ri
x = (wv / 2) - (w / 2)
y = 0
} else {
w = wv
h = w * ri
x = 0
y = (hv / 2) - (h / 2)
}
return CGRect(x: x, y: y, width: w, height: h)
}
為了測試該函數是否正確,我使用了另一個視圖來標識 UIImageView 中的圖像邊框。
let frameIdentifyBorderView = UIView(frame: imageFrameInImageView)
frameIdentifyBorderView.layer.borderWidth = 3
frameIdentifyBorderView.layer.borderColor = UIColor.blueColor().CGColor
imageView.addSubview(frameIdentifyBorderView)
完美的!
最簡單的方法是使用AVMakeRectWithAspectRatioInsideRect 。 只需導入 AVFoundation,您就可以在一行代碼中獲得矩形。 Swift 4. 一定要導入 AVFoundation。
import UIKit
import AVFoundation
class ViewController: UIViewController {
var imageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imageView.frame = self.view.bounds
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = .blue
imageView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
self.view.addSubview(imageView)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
URLSession.shared.dataTask(with: URL(string: "https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?w=940&h=650&dpr=2&auto=compress&cs=tinysrgb")!) { (data, response, error) in
guard let dt = data else{print("error");return}
DispatchQueue.main.async {
guard let image = UIImage(data: dt) else{print("error");return}
self.imageView.image = image
self.imageView.layoutIfNeeded()
let realImageRect = AVMakeRect(aspectRatio: image.size, insideRect: self.imageView.bounds)
//proof
let view = UIView(frame: realImageRect)
view.layer.borderColor = UIColor.green.cgColor
view.layer.borderWidth = 3
self.view.addSubview(view)
}
}.resume()
}
}
現在很容易
import AVKit
let rect = AVMakeRect(aspectRatio: image.size, insideRect: containerView.bounds);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.