The main idea of ββwhat we are trying to do is that we have a large UIImage and we want to cut it into several parts. The user of the function can pass in the number of rows and the number of columns, and the image will be cropped accordingly (i.e. 3 rows and 3 columns cut the image into 9 parts). The problem is that we are having performance issues when trying to accomplish this using CoreGraphics. The largest grid we need is 5x5, and it takes a few seconds to complete the operation (which registers as a timeout for the user). This, of course, is far from optimal.
My colleague and I spent quite a lot of time on this, and unsuccessfully searched the Internet for answers. None of us have experience with Core Graphics, so I hope that the code will have some kind of stupid error that will help fix our problems. He left you SO users to help us figure it out!
We used the tutorial at http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/ to update our code.
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;
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++)
{
CGRect rect = CGRectMake(xPos, yPos, width, height);
CGRect drawRect = CGRectMake(rect.origin.x * -1,
rect.origin.y * -1,
image.size.width,
image.size.height);
CGContextDrawImage(currentContext, drawRect, image.CGImage);
UIImage* croppedImg = UIGraphicsGetImageFromCurrentImageContext();
PuzzlePiece* newPP = [[PuzzlePiece alloc] initWithImageAndFrameAndID:croppedImg :rect :imageCounter];
[slicedImages addObject:newPP];
imageCounter++;
xPos += (width);
}
yPos += (height);
}
UIGraphicsEndImageContext();
}
ANY advice is highly appreciated!
source
share