裁剪UIImage时的性能问题(CoreGraphics,iOS) [英] Performance issues when cropping UIImage (CoreGraphics, iOS)

查看:156
本文介绍了裁剪UIImage时的性能问题(CoreGraphics,iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们要尝试做的基本想法是,我们有一个很大的UIImage,我们想将其切成几块。该函数的用户可以传递许多行和许多列,并且图像将被相应地裁剪(即3行和3列将图像切成9张)。问题是,在尝试使用CoreGraphics完成此操作时,我们遇到了性能问题。我们需要的最大网格是5x5,并且要花几秒钟才能完成操作(这对用户来说是滞后时间。)当然,这远非最佳。

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 5x5, and it takes several seconds for the operation to complete (which registeres as lagtime to the user.) This is of course far from optimal.

我和我的同事已经花了相当长的时间,但一直没有在网上搜索答案。我们俩都不对Core Graphics拥有丰富的经验,所以我希望代码中有一些愚蠢的错误可以解决我们的问题。 SO用户,该留给您了,请帮我们解决!

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!

我们在 http://www.hive05.com/2008/11/crop-an-image-using-the- iphone-sdk / 作为我们代码修订的基础。

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.

以下功能:

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

任何建议都非常感谢!

推荐答案

originalImageView是IBOutlet ImageView。该图像将被裁剪。

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

#import <QuartzCore/QuartzCore.h>

每个切片周围的白色边框都需要使用QuartzCore,以便更好地理解。

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;您将不再看到原始的ImageView。

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

像这样调用它:

[self prepareSlices:4 :4];

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

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

这篇关于裁剪UIImage时的性能问题(CoreGraphics,iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆