ios 如何将图像放入 Realm 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33299523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:04:49  来源:igfitidea点击:

How to put an image in a Realm database?

iosswift2realm

提问by VivienG

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.

我正在使用 Swift 2 编写一个 iOS 应用程序,我想将帐户的个人资料图片本地保存在 Realm 数据库中。我找不到任何文档或谈论它的人。

Is it possible? And how?

是否可以?如何?

May be is it bad to do that?

这样做可能不好?

回答by ProblemSlover

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.

您可以将图像存储为NSData. 假设您有要在本地存储的图像的 URL,这里是一个如何实现的代码片段。

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 AFNetworkingor AlamofireImage.

请注意:这两个代码示例都不是下载图像的可靠方法,因为存在许多陷阱。在现实世界的应用程序/生产中,我建议使用专门用于此目的的库,例如AFNetworkingAlamofireImage

回答by Geoherna

ProblemSlover's solution updated for Swift 2.2:

针对 Swift 2.2 更新了 ProblemSlover 的解决方案:

Important Note:the only change is that now stringByAppendingPathComponenthas been removed as of Swift 2.1, so you will get an error saying:

重要提示:唯一的变化是现在stringByAppendingPathComponent已从 Swift 2.1 中删除,因此您将收到一条错误消息:

stringByAppendingPathComponent is unavailable: use URLByAppendingPathComponent on NSURL instead.

stringByAppendingPathComponent 不可用:在 NSURL 上使用 URLByAppendingPathComponent 代替。

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. BUTif you wish to proceed with this solution, the fix for Swift 2.1+ is to explicitly cast documentsPathto NSString:

请记住,虽然它可能很烦人,但它已被删除是有原因的,您应该按照苹果的建议改用 NSURL。但是如果你想继续这个解决方案,Swift 2.1+ 的修复是显式转换documentsPathNSString

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)
    }
}