加载保存到文档目录中的图像只能进行一次,然后再也不会加载 [英] Loading an image saved to the Documents Directory only works once, then never loads again

查看:74
本文介绍了加载保存到文档目录中的图像只能进行一次,然后再也不会加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,用户可以在其中选择要在该应用程序中使用的图像,该图像将被保存,然后加载到表格视图中.我将图像数据保存到documents目录,然后将数据路径保存为Realm对象的属性.然后,我尝试使用保存的路径加载其关联的图像.

I am writing an app where the user chooses images to use in the app, which will be saved and then loaded into a table view. I am saving the data of the image to the documents directory, and then saving the PATH OF THE DATA as an attribute to a Realm object. I am then trying to use the saved path to load its associated image.

现在,我正在像这样保存(我想是成功的,因为它一次加载了图像...)

Right now, I am saving (successfully I think, because it loads the image once...) like this:

let image = imageField.image
let imageData = UIImagePNGRepresentation(image) as NSData?

let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let uuid = UUID().uuidString
let writePath = documentsDirectory.appendingPathComponent("\(uuid).png")

imageData?.write(toFile: writePath, atomically: true)

ownerPhoto.photoPath = writePath  //setting the path attribute of the object
ownerPhoto.owner = newOwner       //creating a relationship between the image and the owner

newOwner.title = titleTextField.text!
newOwner.ownerDescription = ownerDescriptionField.text!
newOwner.photos.append(ownerPhoto)

let realm = try! Realm()

try! realm.write {
    realm.add(newOwner)
    realm.add(ownerPhoto)
//I am creating and saving both the new Owner profile and their first photo at the same time here
}

保存后,将其选择到tableView中.该tableView将显示所有者列表,以及我们刚刚保存的照片作为每个单元格的背景.我正在尝试这样做:

After this is saved, it then segues to a tableView. This tableView will show a list of owners, with the photo we just saved as each cell's background. I am trying to do this with:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! tableCellTripTableViewCell

    let row = indexPath.row

    let firstPhoto = ownersArray[row].photos[0].photoPath
    //This is an array of all of the Owner objects of the app
    //firstPhoto will be equal to the path (String) that we just saved above

    cell.backgroundImage.image = UIImage(contentsOfFile: firstPhoto)
    return cell
}

当前,单元格仅在首次保存后显示保存的图像.一旦我按保存并选择到tableView,新创建的所有者将具有其背景,但是一旦我重新启动该应用程序,该单元格将没有背景.为什么消失了?我在做什么错了?

Presently, the cell shows the saved image ONLY after it is first saved. Once I press save and it segues to the tableView, the newly created Owner will have their background, but ONCE I RESTART THE APP, the cell has no background. Why is it disappearing? What am I doing wrong?

谢谢!

编辑以便将来阅读的人:

EDIT FOR ANYONE READING THIS IN THE FUTURE:

要解决此问题,请不要保存路径.只是文件名(uuid).也是这样:

To fix this issue, do not save the path. Just the filename (uuid). So do:

ownerPhoto.photoPath = "\(uuid).png"

然后在cellForRowAt函数中

Then at cellForRowAt function:

let imagePath = getDocumentsDirectory().appendingPathComponent(firstPhoto)

cell.backgroundImage.image = UIImage(contentsOfFile: imagePath)

推荐答案

从不存储完整路径.应用的沙箱会随时间变化.只需存储文件名即可.如果要获取文件的完整路径,请在运行时根据存储的文件名和Documents文件夹的当前值进行计算.

Never store a full path. An app's sandbox changes over time. Just store the file name. When you want to get the full path to the file, calculate it at runtime based on the stored filename and the current value for the Documents folder.

这篇关于加载保存到文档目录中的图像只能进行一次,然后再也不会加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆