Ios UIImageView获取显示图像的位置

Ios UIImageView获取显示图像的位置,ios,objective-c,uiimageview,uiimage,Ios,Objective C,Uiimageview,Uiimage,我有一个显示UIImage的UIImageView UIImage可能会更改为不同大小的其他UIImage,其中UIImage的位置和大小也会随之更改 我的问题是,我正在尝试添加一个视图,该视图将位于UIImage的末尾(它会一直更改),而我所能得到的只是UIImageView的帧(它一直保持全屏) 如何获取当前显示UIImage的“帧”?以下内容将回答您的问题,假设您的UIImageView使用UIViewContentModeAspectFit: 您必须考虑UIImageView中图像的大小

我有一个显示UIImage的UIImageView

UIImage可能会更改为不同大小的其他UIImage,其中UIImage的位置和大小也会随之更改

我的问题是,我正在尝试添加一个视图,该视图将位于UIImage的末尾(它会一直更改),而我所能得到的只是UIImageView的帧(它一直保持全屏)


如何获取当前显示UIImage的“帧”?

以下内容将回答您的问题,假设您的UIImageView使用UIViewContentModeAspectFit:

您必须考虑UIImageView中图像的大小。这取决于您如何设置contentMode。根据您的描述,我假设您使用的是UIViewContentModeAspectFit。生成的图像也将集中在UIIVIEVIEW中,因此您也必须考虑这一点。p>
-(CGRect )calculateClientRectOfImageInUIImageView:(UIImageView *)imgView
{
    CGSize imgViewSize=imgView.frame.size;                  // Size of UIImageView
    CGSize imgSize=imgView.image.size;                      // Size of the image, currently displayed

    // Calculate the aspect, assuming imgView.contentMode==UIViewContentModeScaleAspectFit

    CGFloat scaleW = imgViewSize.width / imgSize.width;
    CGFloat scaleH = imgViewSize.height / imgSize.height;
    CGFloat aspect=fmin(scaleW, scaleH);

    CGRect imageRect={ {0,0} , { imgSize.width*=aspect, imgSize.height*=aspect } };

    // Note: the above is the same as :
    // CGRect imageRect=CGRectMake(0,0,imgSize.width*=aspect,imgSize.height*=aspect) I just like this notation better

    // Center image

    imageRect.origin.x=(imgViewSize.width-imageRect.size.width)/2;
    imageRect.origin.y=(imgViewSize.height-imageRect.size.height)/2;

    // Add imageView offset

    imageRect.origin.x+=imgView.frame.origin.x;
    imageRect.origin.y+=imgView.frame.origin.y;

    return(imageRect);
}
要更好地说明三种内容模式之间的差异,请参见以下内容:

Swift 4.2和5.0 Swift 3.0 对于小于3.0的Swift 以下是Swift中的上述方法。同样,假设
contentMode
设置为
。如果给定的
imageView
上没有图像,则将返回
CGRectZero

func calculateRectOfImageInImageView(imageView: UIImageView) -> CGRect {
    let imageViewSize = imageView.frame.size
    let imgSize = imageView.image?.size

    guard let imageSize = imgSize where imgSize != nil else {
        return CGRectZero
    }

    let scaleWidth = imageViewSize.width / imageSize.width
    let scaleHeight = imageViewSize.height / imageSize.height
    let aspect = fmin(scaleWidth, scaleHeight)

    var imageRect = CGRect(x: 0, y: 0, width: imageSize.width * aspect, height: imageSize.height * aspect)
    // Center image 
    imageRect.origin.x = (imageViewSize.width - imageRect.size.width) / 2
    imageRect.origin.y = (imageViewSize.height - imageRect.size.height) / 2

    // Add imageView offset
    imageRect.origin.x += imageView.frame.origin.x
    imageRect.origin.y += imageView.frame.origin.y

    return imageRect
}

我建议使用内置函数avmakerect和aspectratio

func AVMakeRectWithAspectRatioInsideRect(_ aspectRatio: CGSize, _ boundingRect: CGRect) -> CGRect
参数:

方面:
要保持的宽高比(纵横比)

boundingRect: 要装入的边框

返回值 返回一个缩放的CGRect,该CGRect保持aspectRatio指定的适合边界Rect的纵横比


让boundingBox=avmakerectwithaspectratoinsiderect(backgroundImage.size,frame)

基于Janusz提供的极其简单的解决方案,我做了以下工作:

let visibleRect = AVMakeRect(aspectRatio: CGSize(width: image.size.width, height: image.size.height), insideRect: self.frame)
if visibleRect.contains(point) {
    // Do something great here...
}

Swift 3.0

我知道已经很晚了,但将来可能会帮助别人。iOS提供了非常简单的内置解决方案。只需要:

导入AVFoundation
让imageRect=AVMakeRect(aspectRatio:image.size,inside:self.imageView.bounds)

没有直接的API来获取这些信息。您需要根据图像视图的帧、图像大小和图像视图的
contentMode
,自己计算它。@rmaddy这听起来很难说。有没有一种更简单的方法来破解它?你可以使用Imageview.image.size.width/height来获得width和height。虽然我不知道origin.x和y属性让我们这样开始,但请发布代码,说明如何将图像加载到ImageView。此解决方案不是假定
UIViewContentModeScaleSpectFit
而不是
UIViewContentModeScaleSpectFill
?如果使用的是
UIViewContentModeScaleAspectFill
,则无需计算。谢谢,是的,当然是..AspectFit。但是,如果需要…AslectFill,您还需要进行一些计算,因为图像可能会在顶部或左侧进行裁剪,以防在不更改“带/高”比率的情况下无法放入UIImageView。这就是…AspectFill和…ScaleToFill之间的区别。@Marcus我不确定我是否理解你的评论。如果使用了
AspectFill
ScaleToFill
,则在这些情况下不需要计算,因为它们都将填充图像视图。两者之间的区别只是它的伸缩方式。前者缩放图像,直到整个图像视图被填充,同时保持纵横比,这意味着图像可以超出帧,而后者将拉伸图像以填充图像视图,而不保持纵横比,因此整个图像将可见。也许我正在考虑在imageview中返回图像可见部分的矩形。我如何使用它在屏幕上仅拍摄图像的屏幕截图?
func AVMakeRectWithAspectRatioInsideRect(_ aspectRatio: CGSize, _ boundingRect: CGRect) -> CGRect
let visibleRect = AVMakeRect(aspectRatio: CGSize(width: image.size.width, height: image.size.height), insideRect: self.frame)
if visibleRect.contains(point) {
    // Do something great here...
}