UIImageView get the position of the showing Image

UIImageView get the position of the showing Image

uiimageview set image swift 4
get image size swift
uiimageview frame
uiimageview swift 4
uiimageview scale image to fit frame
uiimageview size
resize image without stretching ios
uiimage objective-c

I have a UIImageView which shows an UIImage.

The UIImage may change to other UIImage in different size, and the position and the size of the UIImage inside will change according according to it.

My Problem is that i'm trying add a view that will be at the end of the UIImage (which change all the time) and all I can get is the frame of the UIImageView (which stay full screen all the time).

How can i get the "frame" of current showing UIImage ?


The following will answer your question, assuming your UIImageView used UIViewContentModeAspectFit:

You have to regard the image sizing of the image inside UIImageView. This depends on how you set the contentMode. According your description, I assume you are using UIViewContentModeAspectFit. The resulting image will also be centered in the UIImageView so you also have to consider this for the calculation.

-(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);
}

For a better illustration of the differences between the three content modes, see below:

ios UIImageView get the position of the showing Image?, MARK: - Create Rect func calculateRectOfImageInImageView(imageView: UIImageView) -> CGRect { let imageViewSize = imageView.frame.size let imgSize  [UiImageView setContentMode:UIViewContentModeRedraw]; [UiImageView setBounds:CGRectMake( 0, 0, 50, 50)]; I now hot to change it with last two numbers, but there is no difference if first two numbers change. How can I change the position of it like UiImageView.left = 100 UiImageView.top = 50 (Thats what it looks like in .net)


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

        guard let imageSize = imgSize else {
            return CGRect.zero
        }

        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
    }
Swift 3.0
// MARK: - Create Rect
func calculateRectOfImageInImageView(imageView: UIImageView) -> CGRect {
    let imageViewSize = imageView.frame.size
    let imgSize = imageView.image?.size

    guard let imageSize = imgSize, imgSize != nil else {
        return CGRect.zero
    }

    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
}
For Swift < 3.0

Here is the above method in Swift. Again, assuming that contentMode is set to .ScaleAspectFit If there is no image on the given imageView CGRectZero will be returned.

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
}

UIImageView, Any transparency in the image allows the image view's background to show through. class method on UIImage to obtain the localized version of each image. You have to regard the image sizing of the image inside UIImageView. This depends on how you set the contentMode. According your description, I assume you are using UIViewContentModeAspectFit. The resulting image will also be centered in the UIImageView so you also have to consider this for the calculation.


I recommend using built in function AVMakeRectWithAspectRatio.

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

Parameters:

aspectRatio: The width and height ratio (aspect ratio) you want to maintain.

boundingRect: The bounding rectangle you want to fit into.

Return Value Returns a scaled CGRect that maintains the aspect ratio specified by aspectRatio that fits within bounding Rect.

let boundingBox = AVMakeRectWithAspectRatioInsideRect(backgroundImage.size, frame)

paulz/ImageCoordinateSpace: UICoordinateSpace for , If you need to find the size of an aspect fit image inside its image view, I have extension UIImageView { var contentClippingRect: CGRect { guard let image contentClippingRect to read the position and size of the image inside. How to create live playgrounds in Xcode · Displaying a detail screen with  Take image portrait but UIImageView show landscape. Take image portrait but UIImageView show landscape. Skip navigation Sign in. Search. Loading Close. This video is unavailable.


Based on the wonderfully simple solution from Janusz, here's what I did:

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

How to find an aspect fit image's size inside an image view, Before we get too far ahead of ourselves, let's establish why you'd need to To display it, a UIImageView needs to first decode that JPEG into a bitmap. The draw(_:in:) method draws the image at a given size and position,  UIImageView: It’s something like frame of the image or placeholder of the image where we’ll set our UIImage or image. Now open your ViewController.swift file. There are 2 things to add image to your application programmatically. 1. Add image to your “image view”. 2. Add “image view” in your view. In viewDidLoad method write the


Swift 3.0

I know its quite late but might help someone in future. Its very simple and inbuilt solution provided by iOS. Just need to:

import AVFoundation let imageRect = AVMakeRect(aspectRatio: image.size, insideRect: self.imageView.bounds)

Image Resizing Techniques, ImageView is also commonly used to apply tints to an image and The following XML snippet is a common example of using an ImageView to display an image resource: android:scrollbarStyle, Controls the scrollbar style and position. Get the current ColorStateList used to tint the image Drawable,  I have a following image on uiimageview and i want to draw a small dot on that image can somebody tell me how i can do that . i want to create a radar kind a view so i need to easily add a small circle and remove it after a some time i have try drawing on uiimage view but it always create a new uiimage and apply it to uiimageview is there a way to do this ?


ImageView, The Frame of the accessibility element, in screen coordinates. (Inherited from UIView) Thie image to display when the UIImageView is highlighted. Image. Add UIImageView as subview to main view Position UIImageView at the centre of UIView Create an Image With Rounded Corners in Swift Check out this code example that demonstrates how to create an Image with rounded corners.


UIImageView Class (UIKit), Displaying an image in Xamarin.iOS. 04/24/2018; 8 minutes to read +3; In this article. This article covers including an image asset in a Xamarin.iOS app and displaying that image either by using C# code or by assigning it to a control in the iOS Designer.


A UIImageView allow you to add multiple Label (multiple line text support) on it, you can edit, rotate, resize the Label as you want with one finger ,then render the text on Image. Agrume 5.4 8.4 L4 UIImageView-BetterFace-Swift VS Agrume