How to put an image in a Realm database?
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)
}
}
May be it is a bad idea to do that?
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.
Insert image into Realm database in Android
First, convert bitmap to byte array
Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Later, save byte[] into Realm
Notice: Strings and byte arrays (byte[]) cannot be larger than 16 MB (from Realm Documentation)
Field types
Realm supports the following field types: boolean, byte, short, ìnt,
long, float, double, String, Date and byte[]. The integer types byte,
short, int, and long are all mapped to the same type (long actually)
within Realm. Moreover, subclasses of RealmObject and RealmList are supported to model relationships.
Related Topics
Getting Device Orientation in Swift
Using Audiobufferlist with Swift
What Are the Device-Width CSS Viewport Sizes of the Iphone6 and iPhone 6 Plus
How to Make a Vertical Text Uilabel and Uitextview for iOS in Swift
How to Get the Udid in iOS 6 and iOS 7
Missing Private Key in the Distribution Certificate on Keychain
Jenkins - Xcode Build Works Codesign Fails
How to Add Equal Spacing and Equal Width for Button in iOS Auto Layout
Swift 3 Core Data Delete Object
Ios9 - This Application Is Modifying the Autolayout Engine from a Background Thread -- Where
How to Set Image for Bar Button with Swift
Swift Doesn't Convert Objective-C Nserror** to Throws
Fix CSS Hover on Iphone/Ipad/Ipod
Is Grouptableviewbackgroundcolor Deprecated on iOS 6
Force Landscape Viewcontroller in iOS 7