简体   繁体   中英

Load image from the file after rewrite it - UIImage

I have silly problem with loading image from the file. I have two views putted to UITabBarController.

At the first view user can load his image from the Photo Library or Camera. Second view present this photo. This file is on the server. If user doesn't choose his image server sent custom image. If there is uploaded photo it will push user's picture.

When user tap button there is a menu with options. For example we will decide to take picture from the Photo Library. After user took image:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    self.saveUserImage(userID, imageData: UIImagePNGRepresentation(image)!)
    apiManager.userUploadProfile(userID, imageData: UIImagePNGRepresentation(image)!)

    userImageView.image = image
}


func saveUserImage(userUUID: String, imageData: NSData) {
    let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last
    let savePath = path! + "/\(userUUID)-user.png"
    NSFileManager.defaultManager().createFileAtPath(savePath, contents: imageData, attributes: nil)
}

After that point user can see chosen picture and everything is okey. When user change tab, second view will refresh all data on it and again will download all images and data from the server. Unfortunately images that contains old user image doesn't refresh and there is still old photo.

When we come back to the first tab image is going back to old image but after few seconds.

The strangest thing is if I am checking server there is new uploaded image and in the app container it exist too. When I restart the app everything works perfectly.

It looks like this image is saved in the memory and iOS takes old version from RAM or from some other swap. How refresh UIImageView and show current saved image?

EDIT: There is a method which load the image

func userProfilePicture(userId: String) -> UIImage {
    let cacheDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last
    let savePath = cacheDirectory! + "/\(userId)-user.png"

    if NSFileManager.defaultManager().fileExistsAtPath(savePath) {
        if let image = UIImage(named: savePath) {
            return image
        }
    }

    return UIImage(named: "test_avatar")!
}

I can't comment but i was facing a similar issue but i have a question, does the picture get uploaded before the user changes tabs, or after? Even if you call the function to update the database, it takes some time to actually "send the new photo/path to the photo to the database". Here are your options, if the picture is not in the server at the time of the switching tabs, implement a loading ui until it has successfully uploaded and the new, updated value, into the database. An easy way to do that would be to use the SVProgressHUD pod and set the default mask type to .clear which disables user interaction. Option 2, is to pass the actual UIImage via a static variable, or in the prepare for segue method, or through a struct, or any other way and set the picture that needs to be refreshed to the uiimage without getting it from the server only when you switch tabs.

Okey, I found the answer. The problem was in initialize UIImage. We have two methods to load image from file.

First one load the image and cache it in memory. Later it use only a reference to this image data in memory:

let image = UIImage(named: savePath)

Second method load image strictly from file every time when user use that function:

let image = UIImage(contentsOfFile: savePath)

Right now it works perfectly :D

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