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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:28:55+00:00 2026-05-16T18:28:55+00:00

I am struggling to get my custom drawing code to render at the proper

  • 0

I am struggling to get my custom drawing code to render at the proper scale for all iOS devices, i.e., older iPhones, those with retina displays and the iPad.

I have a subclass of UIView that has a custom class that displays a vector graphic. It has a scale property that I can set. I do the scaling in initWithCoder when the UIView loads and I first instantiate the vector graphic. This UIView is shown when the user taps a button on the home screen.

At first I tried this:

screenScaleFactor = 1.0;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
    screenScaleFactor = [[UIScreen mainScreen] scale];
}
// and then I multiply stuff by screenScale

… which worked for going between normal iPhones and retina iPhones, but chokes on the iPad. As I said, you can get to the UIView at issue by tapping a button on the home screen. When run on the iPad, if you display the UIView when at 1X, it works, but at 2X I get a vector graphic that twice as big as it should be.

So I tried this instead:

UPDATE: This block is the one that’s right. (with the corrected spelling, of course!)

screenScaleFactor = 1.0;
if ([self respondsToSelector:@selector(contentScaleFactor)]) { //EDIT: corrected misspellng.
    screenScaleFactor = (float)self.contentScaleFactor;
}
// again multiplying stuff by screenScale

Which works at both 1X and 2X on the iPad and on the older iPhones, but on a retina display, the vector graphic is half the size it should be.

In the first case, I query the UIScreen for its scale property and in the second case, I’m asking the parent view of the vector graphic for its contentsScaleFactor. Neither of these seem to get me where I want for all cases.

Any suggestions?

UPDATE:

Here’s the method in my subclassed UIView (it’s called a GaugeView):

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    CGAffineTransform t0 = CGContextGetCTM(context);
    t0 = CGAffineTransformInvert(t0);
    CGContextConcatCTM(context, t0);

    [needle updateBox];
    [needle draw: context];
}

needle is of class VectorSprite which is a subclass of Sprite which is subclassed from NSObject. These are from a programming book I’m working through. needle has the scale property that I set.

updateBox comes from Sprite and looks like this:

- (void) updateBox {
    CGFloat w = width*scale;
    CGFloat h = height*scale;
    CGFloat w2 = w*0.5;
    CGFloat h2 = h*0.5;
    CGPoint origin = box.origin;
    CGSize bsize = box.size;
    CGFloat left = -kScreenHeight*0.5;
    CGFloat right = -left;
    CGFloat top = kScreenWidth*0.5;
    CGFloat bottom = -top;
    offScreen = NO;
    if (wrap) {
        if ((x+w2) < left) x = right + w2;
        else if ((x-w2) > right) x = left - w2;
        else if ((y+h2) < bottom) y = top + h2;
        else if ((y-h2) > top) y = bottom - h2; 
    }
    else {
        offScreen = 
        ((x+w2) < left) ||
        ((x-w2) > right) ||
        ((y+h2) < bottom) ||
        ((y-h2) > top);
    }
    origin.x = x-w2*scale;
    origin.y = y-h2*scale;
    bsize.width = w;
    bsize.height = h;
    box.origin = origin;
    box.size = bsize;
}    

Sprite also has the draw and drawBody methods which are:

- (void) draw: (CGContextRef) context {

    CGContextSaveGState(context);

    // Position the sprite 
    CGAffineTransform t = CGAffineTransformIdentity;
    t = CGAffineTransformTranslate(t,x,y);
    t = CGAffineTransformRotate(t,rotation);
    t = CGAffineTransformScale(t,scale,scale);
    CGContextConcatCTM(context, t);

    // draw sprite body
    [self drawBody: context];

CGContextRestoreGState(context);
}

- (void) drawBody: (CGContextRef) context {
    // Draw your sprite here, centered
    // on (x,y)

    // As an example, we draw a filled white circle

    if (alpha < 0.05) return;

    CGContextBeginPath(context);
    CGContextSetRGBFillColor(context, r,g,b,alpha);
    CGContextAddEllipseInRect(context, CGRectMake(-width/2,-height/2,width,height));
    CGContextClosePath(context);
    CGContextDrawPath(context,kCGPathFill);
}    
  • 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-16T18:28:55+00:00Added an answer on May 16, 2026 at 6:28 pm

    How, exactly, are you rendering the graphic?

    This should be handled automatically in drawRect: (the context you get should be already 2x). This should also be handled automatically with UIGraphicsBeginImageContextWithOptions(size,NO,0); if available (if you need to fall back to UIGraphicsBeginImageContext(), assume a scale of 1). You shouldn’t need to worry about it unless you’re drawing the bitmap yourself somehow.

    You could try something like self.contentScaleFactor = [[UIScreen mainScreen] scale], with appropriate checks first (this might mean if you display it in an iPad at 2x, you’ll get high-res graphics).

    Fundamentally, there’s not much difference between an iPad in 2x mode and a “retina display”, except that the iPad can switch between 1x and 2x.

    Finally, there’s a typo: @selector(contentsScaleFactor) has an extra s.

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

Sidebar

Related Questions

I am currently struggling to get the following code to compile. First the header
I'm struggling to get around the 404 errors from asp.net mvc beta when deploying
OK, struggling to get a definitive answer to this one... I have an application
I'm struggling with the following problem. I use the jQuery autocomplete plugin to get
I'm struggling to separate the dependencies in the following code: public static SiteConnector ConnectToSite(String
I have been struggling to get a simple DynamicObject example working in .NET 3.5.
I'm struggling to get iPhone OS4 to produce the default tap highlight on a
Im struggling a bit with the UIkeyboard, I have a datePicker, IntervalPicker some custom
I have implemented a custom table view cell. I can get the cell to
The proper height for my custom UITableViewCells depends on their width. Unfortunately, it is

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.