Ideas based on IOS7 's Textkit Foundation!
Because IOS7 will be in the past design and graphics mixed with the method of packaging into the textkit, so we in the iOS7 system can be very simple to achieve picture and text mixed.
Use nsattributedstring to implement processing, convert nsattributedstring to NSData, and store it in a file.
The code is as follows:
@property (Weak, nonatomic) Iboutlet Uitextview *input; Text input Box
@property (Strong, Nonatomic) nsmutableattributedstring * context;//Storage graphics
_context = _input.textstorage; Assign the contents of the text input box to the storage
NSString *imagename = @ "1.jpg";
UIImage *image = [UIImage imagenamed:imagename];//loading pictures by file name, with cache
Nstextattachment *attachment = [[Nstextattachment alloc] Initwithdata:nil Oftype:nil]; Attachment
Attachment.image = image;
nsattributedstring *textattach = [nsattributedstring attributedstringwithattachment:attachment];//attachment converted to Nsattributedstring
Nsrange range = Self.input.selectedRange; Click on the location, insertion point
Nsinteger i = 0;
i = range.location;
[_content Insertattributedstring:textattach atindex:i]; Insert a picture
NSString *path = [(Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES)) objectAtIndex:0] ; Get the address of the Document for the sandbox
NSString *pathfile = [path stringbyappendingpathcomponent:@ "text"]; The file name to save
NSData *data = [_context datafromrange:nsmakerange (0, _content.length) documentattributes:@{ Nsdocumenttypedocumentattribute:nsrtfdtextdocumenttype} Error:nil]; Convert nsattributedstring to NSData
[Data writetofile:pathfile Atomically:yes]; Write file
Read:
@property (Weak, nonatomic) Iboutlet Uitextview *output;
NSData *outputdata = [NSData datawithcontentsoffile:pathfile];
nsattributedstring *temp = [[Nsattributedstring alloc] Initwithdata:outputdata options:@{ Nsdocumenttypedocumentattribute:nsrtfdtextdocumenttype} documentattributes:nil Error:nil]; Read
[_output setattributedtext:temp]; Show content
Note: This does not take into account the effect of image size on the display, it is recommended to set the size when inserting a picture
An attempt at iOS graphics storage