I'm writing an iOS application using Swift 2 and I would like to save profile picture of an account locally in a Realm database. I can't find any documentation or people talking about that.
Is it possible? And how?
May be is it bad to do that?
Along with text, MongoDB Realm allows developers to upload images to a local Realm database. You can either upload the image to an open-source image uploader, like Imgur, and then download it back to cache it, or you can store the image locally in your Realm database.
Realm is an open source object database management system, initially for mobile operating systems (Android/iOS) but also available for platforms such as Xamarin, React Native, and others, including desktop applications (Windows), and is licensed under the Apache License.
Yes definitely you can use Realm for free to save unlimited data locally. Realm is only charging for cloud storage .
You can store images as NSData
. Given you have the URL of an image, which you want to store locally, here is a code snippet how that can be achieved.
class MyImageBlob { var data: NSData? } // Working Example let url = NSURL(string: "http://images.apple.com/v/home/cb/images/home_evergreen_hero_iphone_medium.jpg")! if let imgData = NSData(contentsOfURL: url) { var myblob = MyImageBlob() myblob.data = imgData let realm = try! Realm() try! realm.write { realm.add(myblob) } }
The rule is simple: If the images are small in size, the number of images is small and they are rarely changed, you can stick with storing them in the database.
If there is a bunch images, you are better off writing them directly to the file system and just storing the path of the image in the database.
Here is how that can be done:
class MyImageStorage{ var imagePath: NSString? } let url = NSURL(string: "http://images.apple.com/v/home/cb/images/home_evergreen_hero_iphone_medium.jpg")! if let imgData = NSData(contentsOfURL: url) { // Storing image in documents folder (Swift 2.0+) let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] let writePath = documentsPath?.stringByAppendingPathComponent("myimage.jpg") imgData.writeToFile(writePath, atomically: true) var mystorage = MyImageStorage() mystorage.imagePath = writePath let realm = try! Realm() try! realm.write { realm.add(mystorage) } }
Please note: Both code samples are not reliable methods for downloading images since there are many pitfalls. In real world apps / in production, I'd suggest to use a library intended for this purpose like AFNetworking or AlamofireImage.
ProblemSlover's solution updated for Swift 2.2:
Important Note: the only change is that now stringByAppendingPathComponent
has been removed as of Swift 2.1, so you will get an error saying:
stringByAppendingPathComponent is unavailable: use URLByAppendingPathComponent on NSURL instead.
Please keep in mind that while it may be annoying, it has been removed for a reason and you should follow apple's recommendation to use NSURL instead. BUT if you wish to proceed with this solution, the fix for Swift 2.1+ is to explicitly cast documentsPath
to NSString
:
class MyImageStorage{ var imagePath: NSString? } let url = NSURL(string: "http://images.apple.com/v/home/cb/images/home_evergreen_hero_iphone_medium.jpg")! if let imgData = NSData(contentsOfURL: url) { //Explicitly cast to NSString let documentsPath = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString) let writePath = documentsPath.stringByAppendingPathComponent("myimage.jpg") imgData.writeToFile(writePath, atomically: true) var mystorage = MyImageStorage() mystorage.imagePath = writePath let realm = try! Realm() try! realm.write { realm.add(mystorage) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With