An attempt at iOS graphics storage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.