Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to save drawing ? #65

Open
wangsuikang opened this issue Oct 8, 2016 · 18 comments
Open

how to save drawing ? #65

wangsuikang opened this issue Oct 8, 2016 · 18 comments

Comments

@wangsuikang
Copy link

Hi,
I want to save drawing ,when I finish drawing. I can open it again.Please tell me how to do it .Look forward to your reply .Thanks!

@acerbetti
Copy link
Owner

I think the best way is to apply the protocol NSCoding to each tool, so you can serialize the arrays of steps and apply them again in the future

@TripwireNL
Copy link

TripwireNL commented Oct 30, 2016

Can you give an example on how to do this, I can't seem to figure out how to save a drawing. I'm using Realm as a database and I want to save the drawing in Realm so I can reload it, but I'm stuck at implementing the NSCoding protocol in the Pen tool.

@acerbetti
Copy link
Owner

@TripwireNL
Copy link

Still don't get how to do this. I've added the NSCoding protocol to the AceDrawingPenTool and tried to implement the example you gave. But I don't know when and where to call the encode feature to save it? Can you provide one example on one of the tools how to implement it please?

@acerbetti
Copy link
Owner

If you have implemented the NSCoding protocol correctly, saving it should be very easy. Here is a tutorial
https://www.raywenderlich.com/1914/nscoding-tutorial-for-ios-how-to-save-your-app-data

@TripwireNL
Copy link

TripwireNL commented Jan 2, 2017

Hi acerbetti,

I finally had some time to pick up where I left. I've wrote this code to save the UIBezierPath (As i understand correctly the UIBezierPath adheres to the NSCoding protocol so I didn't add that (just testing to save and load the lines). I've made the pathArray of the AceDrawingView class public so I can access it in my other classes. This is my code as of now but it doesn't work at the moment I see that the data is saved in NSData structure but it's not loading the drawing back.. :

-(NSData *)saveImageAsData {
    NSData *data;
    for (id tool in aceDrawingView.pathArray) {
        if ([tool isKindOfClass:[ACEDrawingPenTool class]]) {
            ACEDrawingPenTool *penTool = (ACEDrawingPenTool *) tool;
            UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:penTool.path];
            data = [NSKeyedArchiver archivedDataWithRootObject:path];
        }
    }
    return data;
}

-(void)loadDrawing:(NSData *) data {
    UIBezierPath *path =  [NSKeyedUnarchiver unarchiveObjectWithData:data];
    CGMutablePathRef cgPath = CGPathCreateMutableCopy(path.CGPath);
    ACEDrawingPenTool *tool = [ACEDrawingPenTool new];
    tool.path = cgPath;
    [aceDrawingView.pathArray addObject:tool];
    [aceDrawingView updateCacheImage:FALSE];
    [aceDrawingView setNeedsDisplay];
}

@JinkProject
Copy link

@Tripwire999 That is the same issue I'm having. I am correctly storing and reloading the drawn paths, but when I run the method updateCacheImage:, nothing happens. @acerbetti Could you give some insight on how to achieve this?

@TripwireNL
Copy link

TripwireNL commented Apr 12, 2017

@JinkProject
Let's say we use the PenTool, I use the following function to load the data back from the NSData we archived with the NSCoding protocol (DrawPathObject is a Realm object):

+(ACEDrawingPenTool *)createPenTool:(DrawPathObject *)drawing {
    UIBezierPath *path = [NSKeyedUnarchiver unarchiveObjectWithData:drawing.drawPath];
    ACEDrawingPenTool *tool = [ACEDrawingPenTool new];
    tool.lineColor = [UIColor colorWithString:drawing.lineColor];
    tool.lineWidth = drawing.lineWidth;
    tool.lineAlpha = drawing.lineAlpha;
    tool.path = CGPathCreateMutableCopy(path.CGPath);
    return tool;
}

And the returned tool is added to the pathArray. After that I just call:

[aceDrawingView updateCacheImage:YES];
[aceDrawingView setNeedsLayout];

@JinkProject
Copy link

@Tripwire999 OK. Cool. Let me try that. Could this not be achieved the same why by just making ACEDrawingPenTool adhere to NSCoding protocol? Why did you use a separate object (DrawPathObject)

@TripwireNL
Copy link

@JinkProject It's because i'm using Realm and i'm not only storing the DrawPath stuff but also some other parts in Realm like lineAlpha, lineWidth, tooltype e.t.c.

@JinkProject
Copy link

@Tripwire999 How did you encode/decode the path?

@JinkProject
Copy link

@Tripwire999 Also, have you made the path variable from the ACEDrawingPenTool object a publicly, readwriteable property? I've done all of these things and I'm still not seeing the drawings on load.

@JinkProject
Copy link

@Tripwire999 NVM. Got it working. Looks great. Thanks for the help!

@JinkProject
Copy link

@acerbetti @Tripwire999 Would you guys be up for submitting a PR that supports storing drawings? We'd like to use this library as a pod in our projects, but we would obviously want to stay up to date on the latest changes.

@acerbetti
Copy link
Owner

Happy to accept and review any PR. But I don't have any bandwidth to maintain the lib right now

@JinkProject
Copy link

@acerbetti Roger that. I'll submit a PR.

@TripwireNL
Copy link

@JinkProject If you need help I'll be willing to lend a hand for saving and loading the drawings. Though I've modified the AceDrawingView code heavily the saving and loading shouldn't be that different than what i've got. Let me know!

@JinkProject
Copy link

@Tripwire999 Any help would be appreciated!

jackywongcw referenced this issue in TripwireNL/ACEDrawingView Sep 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants