如何使用添加的UIBeizerPath保存图像?

时间:2016-01-25 09:59:09

标签: ios objective-c uiimageview calayer

我试图用愚蠢的绘图来保存图像,但是当我保存图像时,只保存原始图像。

我的预期图像是这样的: enter image description here

这是我的图片保存代码:

UIImageWriteToSavedPhotosAlbum(_imgShow.image,
                                   nil,
                                   nil,
                                   nil);

这是我的UIImageView自定义类:

    -(void)awakeFromNib
{
//    drawingView=[[UIView alloc]initWithFrame:self.bounds];
//    drawingView.backgroundColor=[UIColor clearColor];
//    [self addSubview:drawingView];
    clearBeizer=[[NSMutableArray alloc]init];
    [self updateMask];
}
-(void)updateMask
{
    CALayer *maskLayer=[CALayer layer];
    maskLayer.geometryFlipped=YES;
    maskLayer.contents=(id)self.image.CGImage;
    switch (self.contentMode)
    {
        case UIViewContentModeScaleToFill:
            maskLayer.contentsGravity = kCAGravityResize;
            break;
        case UIViewContentModeScaleAspectFit:
            maskLayer.contentsGravity = kCAGravityResizeAspect;
            break;
        case UIViewContentModeScaleAspectFill:
            maskLayer.contentsGravity = kCAGravityResizeAspectFill;
            break;
        case UIViewContentModeCenter:
            maskLayer.contentsGravity = kCAGravityCenter;
            break;
        case UIViewContentModeTop:
            maskLayer.contentsGravity = kCAGravityTop;
            break;
        case UIViewContentModeBottom:
            maskLayer.contentsGravity = kCAGravityBottom;
            break;
        case UIViewContentModeLeft:
            maskLayer.contentsGravity = kCAGravityLeft;
            break;
        case UIViewContentModeRight:
            maskLayer.contentsGravity = kCAGravityRight;
            break;
        case UIViewContentModeTopLeft:
            maskLayer.contentsGravity = kCAGravityTopLeft;
            break;
        case UIViewContentModeTopRight:
            maskLayer.contentsGravity = kCAGravityTopRight;
            break;
        case UIViewContentModeBottomLeft:
            maskLayer.contentsGravity = kCAGravityBottomLeft;
            break;
        case UIViewContentModeBottomRight:
            maskLayer.contentsGravity = kCAGravityBottomRight;
            break;
        default:
            break;

    }
    maskLayer.frame=self.bounds;
//    drawingView.layer.mask=maskLayer;
    self.layer.mask=maskLayer;

}
-(void)setImage:(UIImage *)image
{
    [super setImage:image];
    [self updateMask];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
//    drawingView.frame=self.bounds;
//    drawingView.layer.mask.frame=drawingView.bounds;
    self.frame=self.bounds;
    self.layer.mask.frame=self.bounds;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    startingPoint = [touch locationInView:self];

}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    touchPoint = [touch locationInView:self];

    if (!CGPointEqualToPoint(startingPoint, CGPointZero))
    {
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(touchPoint.x,touchPoint.y)];
        [path addLineToPoint:CGPointMake(startingPoint.x,startingPoint.y)];

        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.path = [path CGPath];
        shapeLayer.strokeColor = [[UIColor blueColor] CGColor];
        if([UIDevice currentDevice].userInterfaceIdiom ==UIUserInterfaceIdiomPad)
        {
            shapeLayer.lineWidth = 7.0;
        }
        else
        {
            shapeLayer.lineWidth = 5.0;

        }

        shapeLayer.fillColor = [[UIColor redColor] CGColor];
        [self.layer addSublayer:shapeLayer];
        [clearBeizer addObject:shapeLayer];
    }
    startingPoint=touchPoint;

    //    [arrLayer addObject:shapeLayer];
    NSLog(@"Touch moving point =x : %f Touch moving point =y : %f", touchPoint.x, touchPoint.y);
}

这是我的自定义类。

2 个答案:

答案 0 :(得分:1)

您只是更改图像的渲染,而不是图像本身的数据,而是编写该数据并期望它反映您的视图渲染。

您可以使用snapshotViewAfterScreenUpdates:this question的结果可以管理您真正希望实现的目标。

答案 1 :(得分:1)

这里你需要做的是拍摄你的窗户的屏幕截图,一旦你获得了屏幕截图,你将获得该图像。但是您需要根据您的要求进行更改。当你截取窗口截图时,你也会得到导航栏和工具栏。因此,您需要拍摄一个屏幕截图,然后拍摄第二个屏幕截图,以获取没有导航栏和工具栏的图像。 用于保存图像pl。访问以下网址:

这就是我所做的:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   UITouch *touch = [touches anyObject];
   endPoint = [touch locationInView:self.IBImgView];

   SPUserResizableView *spResizable = [[SPUserResizableView alloc] initWithFrame:CGRectMake(0,0,100,200)];

   UIView *contentView = [[UIView alloc] initWithFrame:spResizable.frame];
   [contentView setBackgroundColor:[UIColor clearColor]];

   CAShapeLayer *lines = [CAShapeLayer layer];
   lines.path = self.drawPath.CGPath;
   lines.lineCap = kCALineCapSquare;
   [self.drawPath removeAllPoints];
   lines.bounds = CGPathGetBoundingBox(lines.path);
   lines.strokeColor = [UIColor blackColor].CGColor;
   lines.fillColor = [UIColor blackColor].CGColor; /*if you just want lines*/
   lines.lineWidth = 3;
   lines.position = CGPointMake(spResizable.frame.size.width/2.0f-10, spResizable.frame.size.height/2.0f-10);
   lines.anchorPoint = CGPointMake(.5, .5);
   [contentView.layer addSublayer:lines];

   spResizable.contentView = contentView;

   [spResizable hideEditingHandles];    
   [self.view addSubview:spResizable];
   [self.view setNeedsDisplay];
}

保存图片

- (IBAction)btnSaveTapped:(id)sender {
[UIImagePNGRepresentation(self.IBImgView.image) writeToFile:[self saveDataInDirectory:@"Img1"] atomically:YES];
}


- (NSString *) saveDataInDirectory:(NSString *)strFileName {
NSString *strDocPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *strDirName;
NSFileManager *fileManager = [[NSFileManager alloc] init];
    // Original Image
    strDirName = [strDocPath stringByAppendingPathComponent:kDocOriginalImg];
    if (![fileManager fileExistsAtPath:strDirName]) {
        [fileManager createDirectoryAtPath:strDirName withIntermediateDirectories:NO attributes:nil error:nil];
    }
    strDirName = [[strDirName stringByAppendingPathComponent:strFileName] stringByAppendingPathExtension:@"png"];
return strDirName;

}

但请确保您的spResizable视图高于UIImageView。

Taking ScreenShot Programmatically

如果您想使用相同路径重绘图像,然后删除当前路径并绘制新路径,则可以使用SPUserResizableView。但为此你需要将数据存储在字典中进行编辑。

希望这有助于解决您的问题。