Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7962649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:23:52+00:00 2026-06-04T05:23:52+00:00

What is the best way to manually reproduce contentMode = UIViewContentModeScaleAspectFit; without using it?

  • 0

What is the best way to manually reproduce

contentMode = UIViewContentModeScaleAspectFit;

without using it?

I need to scale a UIImageView (inside a scroll view) to fit the aspect ratio. I need to know the new size of the image to draw overlays over it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:23:53+00:00Added an answer on June 4, 2026 at 5:23 am

    Recently, I needed to find the frame of the image inside an ImageView, to add touchable views over that image, this is how I did it:

    -(void)calculateScaleAndContainerFrame{
        if(!imageView || !image) return;
    
        CGSize imageSize = image.size;
        CGSize imageViewSize = imageView.frame.size;
    
        float imageRatio = imageSize.width / imageSize.height;
        float viewRatio = imageViewSize.width / imageViewSize.height;
    
        if(imageRatio > viewRatio){
            scale = imageSize.width / imageViewSize.width;
        }else{
            scale = imageSize.height / imageViewSize.height;
        }
    
        CGRect frame = CGRectZero;
    
        frame.size = CGSizeMake(roundf(imageSize.width / scale), roundf(imageSize.height / scale));
        frame.origin = CGPointMake((imageViewSize.width - frame.size.width) / 2.0, (imageViewSize.height - frame.size.height) / 2.0);
    
        [container setFrame:frame];
    }
    

    I’m pretty sure you can use it as a guide, replacing the imageViewSize with the content size of your scroll view (or the view you want to put your image in).

    Note 1: In my case, I needed to center the view vertically, if you don’t, just set the y to 0 on the line where I set the frame origin. Same for x if you don’t want to center the image horizontally.

    Note 2: This is NOT, by any means, a code you can just plug in into your project and work, you’ll probably have to read it, understand it, and then apply the method to your own project. I don’t have time right now to modify it to your needs.

    Note 3: With that code I managed to get a view perfectly over the image inside a image view that used that content mode, so it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to resize images using .NET, without losing the EXIF
What is the best way to generate a manually int primary key with mysql?
What is the best way to view the HTTP response headers from a request
What is the best practice way of manually iterating (i.e., one at a time
What is best way to navigate select option using next/previous button with jquery? I
What is the best way to set makeprg=xcodebuild in vim? I'm using filetype line
What is the best way to manually generate Primary Keys in Entity Framework 4.1
I'm unclear on the best way to have multiple view models that derive from
hows the best way to display comments/user data from a db using mvc? do
What's the best way to validate a model when using mvc with repository? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.