Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView get the position of the showing Image

Tags:

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 ?

like image 638
Asaf Nevo Avatar asked Oct 13 '14 20:10

Asaf Nevo


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.

How do I find my UIImage path?

if you already have the image i.e. have added the file to your resources, you can use this to get the file path; NSString *string = [[NSBundle mainBundle] pathForResource:@"IMAGE_FILE_NAME" ofType:@"jpg"]; // or ofType:@"png", etc. Save this answer.

What is use of UIImageView?

The class provides controls to set the duration and frequency when animating multiple images. It also allows you to stop and start the animation. All images associated with a UIImageView object should use the same scale.

What is UIImage?

An object that manages image data in your app.


1 Answers

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 } 
like image 82
Xeaza Avatar answered Oct 09 '22 15:10

Xeaza