多点裁剪图像后如何保存UIImage? [英] How to save the UIImage after multi point cropping the image?

查看:49
本文介绍了多点裁剪图像后如何保存UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

如何从CAShapeLayer保存UIImage?如果这是进行多作物种植的正确方法,或者是实现此目的的任何其他简单方法.请提供您的想法,建议,源代码等.随时欢迎您.

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.

推荐答案

尝试将图层渲染到上下文中并根据该上下文创建图像.

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

这篇关于多点裁剪图像后如何保存UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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