多点裁剪后如何保存UIImage ?

时间:2021-11-19 21:20:41

I want to multi point crop on the image(See the image). Its working fine. My problem is after crop the image the how to I save UIImage. I am using the CAShapeLayer for crop image. Below code using for multi point crop.

我想在图像上多点裁剪(见图像)。它的工作好。我的问题是裁剪图像后如何保存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]];
}

How to I save UIImage from the CAShapeLayer? If this is the correct way for multi cropping or any other easy way to achieve this. Please give your ideas, suggestions, source code, etc. Anything always welcome.多点裁剪后如何保存UIImage ?

如何从CAShapeLayer中保存UIImage ?如果这是正确的方法,多种植或任何其他简单的方法来实现这个。请给出你的想法,建议,源代码等等。任何事情都欢迎。

1 个解决方案

#1


2  

Try rendering layer into a context and creating an image from that context.

尝试将图层渲染到一个上下文中,并从这个上下文中创建一个图像。

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

#1


2  

Try rendering layer into a context and creating an image from that context.

尝试将图层渲染到一个上下文中,并从这个上下文中创建一个图像。

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