ios - 裁剪 UIImage 时的性能问题(CoreGraphics、iOS)

标签 ios uikit uiimage core-graphics crop

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

我和我的同事在这方面花了很长时间,并在网上搜索了没有成功的答案。我们都没有 Core Graphics 的经验,所以我希望代码中有一些愚蠢的错误可以解决我们的问题。 SO 用户,请您帮我们解决这个问题!

我们使用了 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();
}

非常感谢任何建议!!

最佳答案

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

这样调用它:

[self prepareSlices:4 :4];

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

关于ios - 裁剪 UIImage 时的性能问题(CoreGraphics、iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10558035/

相关文章:

ios - UIImage 在模拟器上工作,但在设备上 swift 崩溃

iphone - 如何将委托(delegate)设置为类方法

swift - 无法将 NSString 与 Swift 的静态 let 字符串进行比较

ios - 如果选择了 UIButton,则在 Swift (Xcode 9) 中取消选择其他 UIButton

iphone - NSZombieEnabled 不报告导致 EXC_BAD_ACCESS 错误的对象类型

ios - 垂直显示旋转图像

objective-c - DropBox 应用内登录

ios - Xcode 10.2,Swift 5,使用 Release Scheme 构建程序时命令 compileSwift 失败

ios - 在另一个圆形 UIView 中移动一个圆形 UIView

iphone - 改变 UIImage 颜色