0

I'd like to allow users to take photos and attach this photo to a "person"s profile which will be stored in CoreData.

What is the typical way to store this UIImage?

Edited

Overall each table("Person") will contain a few strings and this UIImage.

I don't want the UIImages to take up too much space, so when user takes photo i will have it cropped and saved as small as possible without lose of visual integrity. But this is another method to figure out later.

Any idea of what size range these UIImages would be? I just need a headshot photo for each profile. Like in "contacts" app on iPhone I'd also like to be able to share this "person" with other people via iMessage or email.

So any suggestions on whether this entire "person" file will need to be

100kb store in the same table as the relevant data

1mb store in a separate table attached via a relationship to avoid loading unnecessarily

1mb store on disk and reference it inside of Core Data

Community
  • 1
  • 1
Chameleon
  • 1,608
  • 3
  • 21
  • 39
  • As JPEG data (ie, binary NSData in CoreData) would be a reasonable place to start. (see `UIImageJPEGRepresentation`) If the photos are "large" you may want to keep them on the filesystem as a plain file with just a pathname or etc inside CoreData – Ben Zotto Apr 22 '15 at 04:55
  • My app won't need the photo's to be large just a profile picture so that user can remember who the person is... like "contacts" on iPhone. – Chameleon Apr 22 '15 at 18:03

2 Answers2

2

here is code that will help you to store UIImage in document directory and then store the document path to your database.

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
    if paths.count > 0 {
        if let dirPath = paths[0] as? String {

            let writePath = dirPath.stringByAppendingPathComponent("Image2.png") 
            UIImagePNGRepresentation(image).writeToFile(writePath, atomically: true)
        }
    }
}

Hope this help you.

Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
1

You can use the Transformable type in your Core Data model – that lets you store any object that conforms to NSCoding which includes UIImage. Your image will effectively be in the database, which might be more convenient for your app.

For performance, there's also a setting to allow external storage. Small files, will just be stored as binary right in the database; larger files, will be written to an external file. The benefit of this is it's transparent to you.

Check out this question for some more details: Storing UIImage in Core Data with the new External Storage flag.

Community
  • 1
  • 1
gregheo
  • 4,182
  • 2
  • 23
  • 22
  • having the image in database sounds like the way to go, since I'll be dealing with small images that are only for visual reference. I edited post with a question too long to place in comments. Ty – Chameleon Apr 22 '15 at 18:24