Reputation: 1
I have a UITextView with attributed strings and UIImageViews / UIViews as subviews of the UITextView. I would like to save the entire UITextView (with subviews), so that, it can be later edited. I have come across NSKeyedArchiver from this code snippet :
- (NSData *)dataForView:(UIView *)view
{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:view forKey:@"view"];
[archiver finishEncoding];
return (id)data;
}
But, as per this thread NSKeyedArchiver returning nil for UIView's subviews, NSKeyedArchiver should not be used to save entire UIViews.
Saving as an image,
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:myPath atomically:YES];
does not appear to be the solution, because, app should support re-editing and re-saving.
Saved it in PDF format successfully, but, can we reload PDF back into UITextView for editing ? As per this thread, Displaying PDF content in UITextView, it is not possible !
Would like to very much get a solution for this. Any help will be greatly appreciated.
p.s. cannot subsitute the UITextView into a UIWebView.
Upvotes: 0
Views: 92
Reputation: 5911
Sounds like CoreData is the way to go.
You'll need a simple SQLite database schema that can store text with any relevant text attributes. The images of the UIImageViews can be written to disk in a suitable format, and the x, y values of the UIImageViews can be saved to the database along with the saved image file names.
Upon resumption for editing, you will simply recreate the UITextView and its UIImageView subviews by reading from the SQLite database.
Upvotes: 0