代码之家  ›  专栏  ›  技术社区  ›  Kishore Kumar

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

  •  0
  • Kishore Kumar  · 技术社区  · 8 年前

    我正试图用我愚蠢的绘图保存图像,但当我保存图像时,只有原始图像被保存。

    我期望的图像是: 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 回复  |  直到 8 年前
        1
  •  1
  •   Tobi Nary    8 年前

    你只是在改变 翻译 而不是图像本身的数据,而是写入该数据并期望它反映视图的渲染。

    您可以使用 snapshotViewAfterScreenUpdates: 结果来自 this question 管理你真正想要归档的内容。

        2
  •  1
  •   Dhaivat Vyas    8 年前

    这里你需要做的是对你的窗口进行屏幕截图,一旦你得到屏幕截图你就会得到那张图片。但您需要根据您的要求进行更改。当你拍摄窗口的屏幕截图时,你也会看到导航栏和工具栏。因此,您需要拍摄一个屏幕快照,然后从中拍摄第二个屏幕快照以获得没有导航栏和工具栏的图像。 要保存图像,请访问以下网址:

    以下是我所做的:

    - (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 但为此,您需要将数据存储在字典中进行编辑。

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