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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:47:39+00:00 2026-05-31T17:47:39+00:00

I’ve been trying to create a sprite sheet loader for days now, and I’ve

  • 0

I’ve been trying to create a sprite sheet loader for days now, and I’ve written what I feel to be a pretty solid a pretty decent algorithm for it. However when it comes to drawing the single frame I’ve cut-out of the larger sprite sheet, nothing is drawn, and at the moment I’m trying to identify where the problem is. I strongly suspect it’s in the method I use to cut out the frame. So here it is. (Maybe I over annotate my code a bit)

- (UIImage*) cutOutFrame:(UIImage *)spriteSheet frameDimensions:(CGRect)frameRect 
{ 

//create a context to do our cropping within
UIGraphicsBeginImageContext(frameRect.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();

//now we create a CGRect to crop our new Context down to the size of the sprite we're about to place in it. Make the X&Ycoordinates 0,0 so that we clip at the beginning of the context. Then the height and width need to be the height and width of our sprite.
CGRect rectForContextClip = CGRectMake(0, 0, frameRect.size.width, frameRect.size.height);
//now clip the context to nothing but rectForContextClip
CGContextClipToRect(currentContext, rectForContextClip);

//create a rect the size of the spritesheet to draw out sprite sheet into. We will then put the sprite sheet ontop of our frame-sized context and make a UIImage out of JUST THAT. This CGRect however is mainly to control the offset
//This is important as we need to be drawing the right frame into our context below
CGRect drawRect = CGRectMake(frameRect.origin.x, 
                             frameRect.origin.y, 
                             spriteSheet.size.width, 
                             spriteSheet.size.height);

//draw the spritesheet to our clipped context
CGContextDrawImage(currentContext, drawRect, spriteSheet.CGImage);

//now we simply rip this clipped context with our Frame on it into its own UIImage :D
UIImage* ourFrame = UIGraphicsGetImageFromCurrentImageContext();


//I'm assuming this clears the context and makes it not the current context anymore. Probably doesn't do the memory any harm too.
UIGraphicsEndImageContext();

NSLog(@"ourFrame dimensions /nwidth = %f/nheight = %f",ourFrame.size.width, ourFrame.size.height);

return ourFrame;
}

This cutOutFrame method is then used within another function that loops through the sprite sheet and creates an array of UIImages, each containing just one of the images contained in the sprite sheet. That method is below if anybody feels it could have a hand in the problem.

- (void) chopSpriteSheet:(UIImage*) sheetToChop
{
    //create a dictionary to hold the dictionary "frames" we loaded in from the plist file
    if(!sheetFrameData)
    {
    sheetFrameData = [[NSDictionary alloc] initWithDictionary:[coordinates valueForKey:@"frames"]];
    }

    //create an array to hold all the keys in sheetFrameData for us to iterate through
    NSArray* framesKeysArray = [[NSArray alloc]initWithArray:[sheetFrameData allKeys]];

    //loop through the elements in framesKeyArray, get the info at that key within sheetFrame Data
    for (int i=0; i<[framesKeysArray count]; i++) 
    {
            //TODO cycle through, create the UIimages for each frame and load them into the NSArray sheetFrames//UPDATE load
            //TOBEDONE with the cutOutFrame function then into the frames dictionary

        //ensure the frames dictionary is not Nil and holds enough memory to accomodate for all the images in the sprite sheet 

        //not to be confused with the frames dictionary from the plist file
        if (!frames)
        {
            frames = [[NSMutableDictionary alloc] initWithCapacity:[framesKeysArray count]];
        }

        //Shove the values found in the dictionary at the location of the key given by the corresponding entry in the framesKeyArray into the cutOutFrame function
        UIImage* frameToArray = [self cutOutFrame:sheetToChop frameDimensions:CGRectMake(
            [[[sheetFrameData valueForKey:[framesKeysArray objectAtIndex:i]] valueForKey:@"x"] floatValue], 
            [[[sheetFrameData valueForKey:[framesKeysArray objectAtIndex:i]] valueForKey:@"y"] floatValue], 
            [[[sheetFrameData valueForKey:[framesKeysArray objectAtIndex:i]] valueForKey:@"width"] floatValue],
            [[[sheetFrameData valueForKey:[framesKeysArray objectAtIndex:i]] valueForKey:@"height"] floatValue])];


        we'll then stick it all into the frames dictionary, which stores all of them as GLTextures

        [frames setValue:[[GLTexture alloc] initWithImage:frameToArray] forKey:[NSString stringWithFormat:@"frame%i", i]];



    }
    //release framesKeyArray 
    [framesKeysArray release];



}

So that’s what I have. Can you see anything in here that would cause the UIImage to be drawn blank?

P.S. in case you were wondering GLTexture is the class i am using to draw the UIImage using openGL. But Using other examples I’ve already proven to myself that this class works.

  • 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-31T17:47:40+00:00Added an answer on May 31, 2026 at 5:47 pm

    I think you want to do this:

    CGRect drawRect = CGRectMake(-frameRect.origin.x, 
                                 -frameRect.origin.y, 
                                 spriteSheet.size.width, 
                                 spriteSheet.size.height);
    

    That is, draw the full image offset down and to the left so that the section you are clipping falls within the current context.

    (Also, the CGContextClipToRect is unnecessary because the context will automatically clip to its bounds)

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
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.