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

  • Home
  • SEARCH
  • 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 8021887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:06:55+00:00 2026-06-04T22:06:55+00:00

The basic idea of what we are trying to do is that we have

  • 0

The basic idea of what we are trying to do is that we have a large UIImage, and we want to slice it into several pieces. The user of the function can pass in a number of rows and number of columns, and the image will be cropped accordingly (ie. 3 rows and 3 columns slices the image into 9 pieces). The problem is, we’re having performance issues when trying to accomplish this with CoreGraphics. The largest grid we require is 5×5, and it takes several seconds for the operation to complete (which registeres as lagtime to the user.) This is of course far from optimal.

My colleague and I have spent quite a while on this, and have searched the web for answers unsuccessfully. Neither of us are extremely experienced with Core Graphics, so I’m hoping there’s some silly mistake in the code that will fix our problems. It’s left to you, SO users, to please help us figure it out!

We used the tutorial at http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/ to base revisions of our code on.

The function below:

-(void) getImagesFromImage:(UIImage*)image withRow:(NSInteger)rows withColumn:(NSInteger)columns
{
    CGSize imageSize = image.size;
    CGFloat xPos = 0.0;
    CGFloat yPos = 0.0;
    CGFloat width = imageSize.width / columns;
    CGFloat height = imageSize.height / rows;

    int imageCounter = 0;

    //create a context to do our clipping in
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGRect clippedRect = CGRectMake(0, 0, width, height);
    CGContextClipToRect(currentContext, clippedRect);

    for(int i = 0; i < rows; i++)
    {
        xPos = 0.0;
        for(int j = 0; j < columns; j++)
        {
            //create a rect with the size we want to crop the image to
            //the X and Y here are zero so we start at the beginning of our
            //newly created context
            CGRect rect = CGRectMake(xPos, yPos, width, height);

            //create a rect equivalent to the full size of the image
            //offset the rect by the X and Y we want to start the crop
            //from in order to cut off anything before them
            CGRect drawRect = CGRectMake(rect.origin.x * -1,
                                     rect.origin.y * -1,
                                     image.size.width,
                                     image.size.height);

            //draw the image to our clipped context using our offset rect
            CGContextDrawImage(currentContext, drawRect, image.CGImage);

            //pull the image from our cropped context
            UIImage* croppedImg = UIGraphicsGetImageFromCurrentImageContext();


            //PuzzlePiece is a UIView subclass
            PuzzlePiece* newPP = [[PuzzlePiece alloc] initWithImageAndFrameAndID:croppedImg :rect :imageCounter];
            [slicedImages addObject:newPP];

            imageCounter++;
            xPos += (width);
        }
        yPos += (height);
    }
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
}

ANY advice greatly appreciated!!

  • 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-04T22:06:56+00:00Added an answer on June 4, 2026 at 10:06 pm

    originalImageView is an IBOutlet ImageView. This image will be cropped.

    #import <QuartzCore/QuartzCore.h>
    

    QuartzCore is needed for the white border around each slice for better understanding.

    -(UIImage*)getCropImage:(CGRect)cropRect
    {
    CGImageRef image = CGImageCreateWithImageInRect([originalImageView.image CGImage],cropRect);
    UIImage *cropedImage = [UIImage imageWithCGImage:image];
    CGImageRelease(image);
    return cropedImage;
    }
    
    -(void)prepareSlices:(uint)row:(uint)col
    {
    float flagX = originalImageView.image.size.width / originalImageView.frame.size.width;
    float flagY = originalImageView.image.size.height / originalImageView.frame.size.height;
    
    float _width    = originalImageView.frame.size.width / col;
    float _height   = originalImageView.frame.size.height / row;
    
    float _posX = 0.0;
    float _posY = 0.0;
    
    for (int i = 1; i <= row * col; i++) {
    
        UIImageView *croppedImageVeiw = [[UIImageView alloc] initWithFrame:CGRectMake(_posX, _posY, _width, _height)];
        UIImage *img = [self getCropImage:CGRectMake(_posX * flagX,_posY * flagY, _width * flagX, _height * flagY)];
        croppedImageVeiw.image = img;
    
        croppedImageVeiw.layer.borderColor = [[UIColor whiteColor] CGColor];
        croppedImageVeiw.layer.borderWidth = 1.0f;
    
        [self.view addSubview:croppedImageVeiw];
        [croppedImageVeiw release];
    
        _posX += _width;
    
        if (i % col == 0) {
            _posX = 0;
            _posY += _height;
        }
    
    }
    
    originalImageView.alpha = 0.0;
    
    }
    

    originalImageView.alpha = 0.0; you won’t see the originalImageView any more.

    Call it like this:

    [self prepareSlices:4 :4];
    

    It should make 16 slices addSubView on self.view. We have a puzzle app. This is working code from there.

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

Sidebar

Related Questions

the basic idea is that you have some class that has a reference type
I have some trouble trying to split large files (say, around 10GB). The basic
the basic idea is that I want to link to path that's relative to
I have several questions involving the interaction between JavaScript and MySQL. The basic idea
References in C++ are baffling me. :) The basic idea is that I'm trying
The basic idea is to iterate through a directory full of .plists which have
I have basic idea on Kilo Virtual Machine on Mobiles , I have clear
I have some basic idea on how to do this task, but I'm not
I have a basic implementation of alpha-beta pruning but I have no idea how
http://jsfiddle.net/Lqvcr/53/ This is so damn basic and I have no idea how to make

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.