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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:49:35+00:00 2026-06-10T16:49:35+00:00

In my iPad drawing app, I have a 768 x 1024 buffer context that

  • 0

In my iPad drawing app, I have a 768 x 1024 buffer context that I create and use to speed up my drawing.

Here is the code I’m currently executing when drawRect is called that simply copies my already existing drawing onto the screen:

CGContextRef context = UIGraphicsGetCurrentContext();

CGImageRef image = CGBitmapContextCreateImage([DrawingState sharedState].context);
CGContextDrawImage(context, CGRectMake(0, 0, 768, 1024), image);
CGImageRelease(image);

This code works just as expected: it collects and image from my background context and draws it into my current context, then releases the image. It works!

However, I’m doing some performance tuning to speed up my drawing and I’m trying this code instead, where “rect” is the rectangle passed in by drawRect

CGContextRef context = UIGraphicsGetCurrentContext();

CGImageRef image = CGBitmapContextCreateImage([DrawingState sharedState].context);
CGImageRef subImage = CGImageCreateWithImageInRect(image, rect);
CGContextDrawImage(context, rect, subImage);
CGImageRelease(subImage);
CGImageRelease(image);

This does not work; the image does not appear in the correct location, and may not even be the correct image (can’t tell since if the image appears in the wrong location, a portion of the image won’t even draw since it will be outside of drawRect’s rectangle.) Any idea what is going on?

Below is how I am initializing the [DrawingState sharedState].context. This part should be fine, but I figured I’d include it for completeness.

if(context != NULL)
{
    CGContextRelease(context);
    context = NULL;
}
if(bitmapData != NULL)
{
    free(bitmapData);
    bitmapData = NULL;
}
if(colorSpace != NULL)
{
    CGColorSpaceRelease(colorSpace);
    colorSpace = NULL;
}

CGSize canvasSize = CGSizeMake(screenWidth, screenHeight);

int bitmapByteCount;
int bitmapBytesPerRow;

bitmapBytesPerRow   = (canvasSize.width * 4);
bitmapByteCount     = (bitmapBytesPerRow * canvasSize.height);

colorSpace = CGColorSpaceCreateDeviceRGB();

bitmapData = malloc( bitmapByteCount );

if( bitmapData == NULL ){
    NSLog(@"Buffer could not be alloc'd");
}

//Create the context
context = CGBitmapContextCreate(bitmapData, canvasSize.width, canvasSize.height, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst);

As requested, the actual optimization I am trying to perform is below. I am drawing a buffer image (before doing additional drawing) into my buffer context, then later copying my buffer context into the current context.

Original Code:

CGContextClearRect([DrawingState sharedState].context, CGRectMake(0, 0, 768, 1024));
if(bufferImage != NULL)
{
    CGContextDrawImage([DrawingState sharedState].context, CGRectMake(0, 0, 768, 1024), bufferImage);
}

Optimized Code:

CGContextClearRect([DrawingState sharedState].context, rect);
if(bufferImage != NULL)
{
    CGImageRef subImage = CGImageCreateWithImageInRect(bufferImage, rect);
    CGContextDrawImage([DrawingState sharedState].context, rect, bufferImage);
    CGImageRelease(subImage);
}
  • 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-10T16:49:36+00:00Added an answer on June 10, 2026 at 4:49 pm

    The short version of the answer to this question was that I wanted to use CGContextClipToRect instead of trying to get sub images. CGContextClipToRect(CGContextRef, CGRect) is used to automatically clip any drawing to a context outside the supplied rectangle.

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

Sidebar

Related Questions

I'm designing an iPad app that will have a custom grid in it. The
Currently I'm workign on a drawing app for the iPad. I need to reposition
I'm currently making an OpenGL ES 2.0 drawing app for the iPad and I
I am working on a iPad drawing app.. were i have a UIView class
Let's say I have a UITableView that is part of an iPad app doesn't
I have an iPad app that has draggable UIViews contained in a larger parent
I have an iPad app that uses the storyboard board feature and then I
I am working on a drawing app, with pen touch, on iphone and ipad.
I'm trying to create a drawing tool set for the iPad and so far
In my iPad app, I have created a rich text editor i.e some rich

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.