简体   繁体   中英

Saving Image Path From Documents Directory in Core Data, Retrieving Only Partial Image. Swift

I am working on creating an app with Swift that will save and retrieve images both using CloudKit and Core Data . With CloudKit I will save the image as a CKAsset and with Core Data I save the image in the documents directory, and then store the path name in Core Data . When my app starts loading it will first look for any items stored locally, then check the cloud if no local data is stored (saves a lot of loading time). Anyways, when I load the images from CloudKit they are fine, and the same goes for Core Data and the documents directory path most of the time . I've been noticing this problem periodically when images are loaded in Core Data using their path name, essentially the image comes back only partially, some of it is grayed out as if it stopped drawing 85% of the way through or something. It's hard to find a good example because it doesn't happen all the time, but here is the best one I could find of my cat: 猫的局部影像

See how approximately 15% of the image at the top is just grey? It's never localized to any specific part of an image, happens on the top, left, right, bottom, even in the middle. And like I said it is not at all a chronic issue. Here is my code used to store the image in the documents directory, and save the path name in Core Data :

// Make the unique path for the Image Name and save it
let userImageName:String = "savedUserImage\(indexForImageName).jpg"
let imagePath = saveInDocumentsDirectory(userImageName)

// Save the game's reference image in the documents directory
self.saveImage(user.referenceImage, path: imagePath)

func saveImage (image: UIImage, path: String ) {

    let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
    jpgImageData?.writeToFile(path, atomically: true)

}

func documentDirectoryURL() -> NSURL {
    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    return documentsURL
}

func saveInDocumentsDirectory(filename: String) -> String {

    let fileURL = documentDirectoryURL().URLByAppendingPathComponent(filename)
    return fileURL.path!

}

Then later upon loading the app, here is the code I use to retrieve my image path names, and then fetch the image from the documents directory:

if let image = newUser.imageName {
    let imagePath = saveInDocumentsDirectory(image)

    if let loadedImage = loadImageFromPath(imagePath) {

        user.referenceImage = loadedImage

    } else { self.customDelegate.alertUserErrorLoadingImage() }
}

func loadImageFromPath(path: String) -> UIImage? {

    let image = UIImage(contentsOfFile: path)

    if image == nil {

        self.customDelegate.alertUserErrorLoadingImage()
    }

    return image

}

If anybody has any good ideas about how to fix this problem, or even an idea as to why this is happening, I would greatly appreciate it. My first thought is that since I'm using CloudKit and Core Data , that maybe at some point my app is saving an image that is in the process of being asynchronously loaded via CloudKit , and then getting interrupted somehow...but based on how I've programmed it that shouldn't ever happen. I'm curious to see if this is a common problem, or if anyone else has experienced, and conquered it. Thank you in advance!

I found this in notification extensions, and I had to do something like this. No idea why.

let data = try! Data(contentsOf: attachment.url)
self.imageView.image = UIImage(data: data) //Must use data, doesn't work right with loading straight into image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM