samedi 11 avril 2015

xcode/ios: How to save image to disk and store name in core data

first time working with selecting and taking photos. I have code working that lets a user choose (or take) a profile pic and it displays in uiview in app using the delegate pattern. To persist this pic, many articles suggest you store the image on disk, not in core data, and store just the file name in core data.


I have found the following method for storing the files.



- (void)saveImage: (UIImage*)image
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//create a file name perhaps using time and call it
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: @"profilefilename.png"] ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}


I am clueless, however, how and where to call this method to store the file on disk and also when and where to store in core data.


Thanks for any suggestions


Aucun commentaire:

Enregistrer un commentaire