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 ?
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.
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.
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.
An object that manages image data in your app.
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 }
// 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 }
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With