3

我想对图像进行多点裁剪(见图)。它工作正常。我的问题是裁剪图像后如何保存UIImage。我正在使用CAShapeLayer裁剪图像。下面的代码用于多点裁剪。

- (void)multiPointCrop:(CGPoint)cropPoint
{
    UIBezierPath *aPath = [UIBezierPath bezierPath];
    [aPath moveToPoint:cropPoint];

    for (NSString *pointString in self.touchPoints) { 
        if ([self.touchPoints indexOfObject:pointString] != 0)
            [aPath addLineToPoint:CGPointFromString(pointString)];
    }
    [aPath addLineToPoint:cropPoint];
    [aPath closePath];

    [self setClippingPath:aPath andView:self];
    [self setNeedsDisplay];
}

- (void)setClippingPath:(UIBezierPath *)clippingPath andView:(UIView *)view;
{
    if (![[view layer] mask])
        [[view layer] setMask:[CAShapeLayer layer]];

    [(CAShapeLayer*) [[view layer] mask] setPath:[clippingPath CGPath]];
}

如何UIImage保存CAShapeLayer?如果这是多重裁剪的正确方法或任何其他简单的方法来实现这一点。请给出你的想法、建议、源代码等。任何东西都欢迎。在此处输入图像描述

4

1 回答 1

2

尝试将图层渲染到上下文中并从该上下文创建图像。

CALayer *layer = view.layer;
CGSize s = layer.frame.size;
// create context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, s.width, s.height,
                                             8, (s.width * 4),
                                             colorSpace, kCGImageAlphaPremultipliedLast);

// flip Y
CGContextTranslateCTM(context, 0.0, s.height);    
CGContextScaleCTM(context, 1.0, -1.0);

// render layer
[layer renderInContext:context];

CGImageRef imgRef = CGBitmapContextCreateImage(context);
// here is your image
UIImage *img = [UIImage imageWithCGImage:imgRef];  

// release owned memory
CGImageRelease(imgRef);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
于 2012-10-25T08:20:40.217 回答