裁剪UIImage(CoreGraphics,iOS)时的性能问题

我们要做的事情的基本思想是我们有一个大的UIImage,我们想把它分成几块。 该函数的用户可以传递多行和多列,并且图像将被相应地裁剪(即,3行和3列将图像分割成9个)。 问题是,我们遇到性能问题,当试图用CoreGraphics完成这个。 我们需要的最大网格是5×5,完成操作需要几秒钟的时间(注册为用户的滞后时间),这当然远非最佳。

我和我的同事在这方面花了一段时间,并且在网上search失败的答案。 我们都不是非常有经验的核心graphics,所以我希望在代码中有一些愚蠢的错误,将解决我们的问题。 这是留给你,所以用户,请帮助我们搞清楚!

我们使用http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/上的教程来修改我们的代码。

下面的function:

-(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(); } 

任何build议非常感谢!

originalImageView是一个IBOutlet ImageView。 这个图像将被裁剪。

 #import <QuartzCore/QuartzCore.h> 

为了更好的理解,每个切片周围的白色边框需要QuartzCore。

 -(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; 你将不会再看到originalImageView。

像这样调用它:

 [self prepareSlices:4 :4]; 

它应该在self.view上制作16片addSubView。 我们有一个益智应用程序。 这是来自那里的工作代码。