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 723309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:05:36+00:00 2026-05-14T06:05:36+00:00

I’ve created a custom view class and right now just want to draw an

  • 0

I’ve created a custom view class and right now just want to draw an image scaled to fit the view, given a UIImage. I tried just drawing the UIImage.CGImage, but as others have attested to on this site (and in the docs), that renders the image upside down.

So, at the suggestion of an answer I found to another question, I’m trying to draw it directly, but nothing is rendering in the view and I’m not sure why. Here’s my drawing code:

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];

    if (self.originalImage) {
        [self drawImage];
    }
}

- (void) drawImage {
    if (CGSizeEqualToSize(originalImage.size, self.frame.size) == NO) {
        CGFloat scaleFactor = 1.0;
        CGFloat scaledWidth = 0.0;
        CGFloat scaledHeight = 0.0;
        CGPoint thumbPoint = CGPointMake(0.0, 0.0);

        CGFloat widthFactor = self.frame.size.width / originalImage.size.width;
        CGFloat heightFactor = self.frame.size.height / originalImage.size.height;

        if (widthFactor < heightFactor) {
            scaleFactor = widthFactor;
        } else {
            scaleFactor = heightFactor;
        }

        scaledWidth = originalImage.size.width * scaleFactor;
        scaledHeight = originalImage.size.height * scaleFactor;

        if (widthFactor < heightFactor) {
            thumbPoint.y = (self.frame.size.height - scaledHeight) * 0.5;
        } else if (widthFactor > heightFactor) {
            thumbPoint.x = (self.frame.size.width - scaledWidth) * 0.5;
        }

        UIGraphicsBeginImageContext(self.frame.size);
        CGRect thumbRect = CGRectZero;
        thumbRect.origin = thumbPoint;
        thumbRect.size.width = scaledWidth;
        thumbRect.size.height = scaledHeight;

        [originalImage drawInRect:thumbRect];
        self.scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    } else {
        self.scaledImage = originalImage;
    }   
}

My understanding (after studying this a bit) is that the UIGraphicsBeginImageContext function creates an offscreen for me to draw into, so now how do I render that context on top of the original one?

  • 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-05-14T06:05:37+00:00Added an answer on May 14, 2026 at 6:05 am

    I commended out the calls to GraphicsBegin and GraphicsEnd and the image renders – but many things that I read indicated that one should create a graphics context in this way.

    If those were the names, then it would make sense that you would need to call them before and after drawing.

    But those are not the names. The full names of the functions, as you used them in your code, are:

    • UIGraphicsBeginImageContext
    • UIGraphicsEndImageContext

    These names hint that these functions are for a specific kind of drawing, and the documentation backs that up:

    You use this function to configure the drawing environment for rendering into a bitmap. …

    While the context created by this function is the current context, you can call the UIGraphicsGetImageFromCurrentImageContext function to retrieve an image object based on the current contents of the context.

    That is what these functions are for: Making a new image by capturing some drawing.

    Your situation is different: You already have an image, and you are a view that has been called upon to draw. This means that you already have a current context, and you need only to draw into it.

    So, don’t create a context—just draw.

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

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.