什么,我们正在尝试做的基本想法是,我们有一个大的UIImage,我们希望把它切成几片。 该功能的用户可以通过在许多行和列的数目,以及图像将相应地裁剪(即3行3列的切片图像分割成9个)。 问题是,我们试图用CoreGraphics中做到这一点的时候有性能问题。 我们需要最大的网格是5x5的,这需要几秒钟的操作完成(这registeres作为滞后时间给用户。)这当然是远远没有达到最佳。
我和我的同事都花在这个相当长的一段,并寻找答案的网页失败。 我们都不是非常核芯显卡经历过,所以我希望有一个在会解决我们问题的代码的一些愚蠢的错误。 它留给你的,所以用户,请帮助我们看着办吧!
我们使用的教程在http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/立足的我们的代码修改。
下面的功能:
-(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();
}
任何意见非常感谢!