ios - Saving Photos to Core Data -


i want save photo core data. in internet , find many resources core data cannot find 1 want accomplish. want use core data save images user has taken , want user select category wants save it. sorting pictures categories example meat or vegetables or etc. have made 1 entity called photo , put 6 attributes name of categories example meat vegetables. did not select type yet. thinking user chooses category , save attribute. here's question. want cannot find resources meet want do. doing correct thinking of using multiple attributes?

scroll down storing uiimage data in core data, firstly bit core data attributes: shouldn't use multiple attributes different categories, should have single attribute called 'category'. integer or string, depending on implementation. recommend using integer , defining enum follows:

typedef ns_enum(nsuinteger, myfoodcategory){     myfoodcategoryvegetable = 0,     myfoodcategorymeat = 1,     myfoodcategoryfish = 2,     myfoodcategorysweet = 3, }; 

when print out view, following:

-(void)setlabeltextforcategory:(myfoodcategory)category {     switch(category) {         case myfoodcategoryvegetable:             self.categorylabel.text = @"vegetable";             break;         case myfoodcategorymeat:             self.categorylabel.text = @"meat";             break;         case myfoodcategoryfish:             self.categorylabel.text = @"poisson"; //french             break;         case myfoodcategorysweet:             self.categorylabel.text = @"dessert";             break;     } } 

this means if change mind test wish print each category, have change in place, , don't have change in core database. example, use localization display different language or variant of same word.

storing uiimage data in core data

ok, answer main question. avoid storing image data in core data, save disk instead. using nsfilemanager class or convert uiimage nsdata , write file, e.g.

uiimage *image = ... nsdata *imagedata = uiimagepngrepresentation(image); // or use uiimagejpegrepresentation if it's jpeg image  nsstring *documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject]; nsstring *filename = @"imagewhatever1"; nsstring *filepath = [documentspath stringbyappendingpathcomponent:filename]; nserror *writeerror = nil; if (![data writetofile:filepath options:nsdatawritingatomic error:&writeerror]) {     nslog(@"failed write file: %@", writeerror); } 

however, if prefer save image directly core data, create attribute called 'imagedata' , set attribute type 'binary data'. save nsdata object core data entity. when read nsdata object, can convert uiimage follows:

uiimage *imageloadedfromdiskorcoredata = [uiimage imagewithdata:imagedata]; 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -