How can I get the value from iOS Swift 3 completion handler

Can anyone suggest or direct how I can return what I ProfileImagereceived in the Location 1code below and return it to Location 2. Many thanks for your help. I went, other questions, but none of them helped me.

static var profileImage : UIImage{

    get{
        let defaults        = UserDefaults.standard

        guard let imageData = defaults.object(forKey: "profileImage") as? NSData else{
            downloadAndSetProfileImage(completionHandler: { (profileImage) in

               // LOCATION 1:
               // PLEASE ADVISE HOW I CAN RETURN THE OBTAINED PROFILE IMAGE BELOW at LOCATION 2
            })

        }

        // LOCATION 2:
        // I would like to return the profileImage Here i.e. return profileImage

    }
    set (image){

        let defaults        = UserDefaults.standard
        let imageData : NSData = UIImagePNGRepresentation(image)! as NSData
        defaults.set(imageData, forKey: "profileImage")
    }
}
+4
source share
3 answers

. profileImage , async, , , , . :

static var profileImage : UIImage? {
    get {
        let defaults = UserDefaults.standard
        guard let imageData = defaults.object(forKey: "profileImage") as? NSData else {
            return nil
        }
        return UIImage(data: Data(referencing: imageData))
    }
    set {
        let defaults = UserDefaults.standard
        guard newValue != nil else {
            defaults.removeObject(forKey: "profileImage")
            return
        }
        let imageData = NSData(data: UIImagePNGRepresentation(newValue!)!)
        defaults.set(imageData, forKey: "profileImage")
    }
}

// Async function to retrieve profile image
func getProfileImage(completion: (_ image: UIImage?) -> ()) {
    guard ProfileAPI.profileImage == nil else {
        completion(ViewController.profileImage!)
        return
    }

    // Your image dowload funciton
    SomeImageDownloader.downloadImage("imagePath") { downloadedImage in
        completion(downloadedImage) // assuming downloadedImage can be nil
    }
}

, :

getProfileImage { (image) in
    if let profileIage = image {
        // do something with it
    }
}

getProfileImage profileImage downloadImage. getProfileImage , , downloadImage . : , , - , .

+1
static var profileImage : UIImage{

get{
    let defaults        = UserDefaults.standard

    guard let imageData = defaults.object(forKey: "profileImage") as? NSData else{
        downloadAndSetProfileImage(completionHandler: { (profileImage) in

           // LOCATION 1:
           return profileImage
        })

    }

    // LOCATION 2:
   return UIImage(data: (imageData as! NSData) as Data)

}
set (image){

    let defaults        = UserDefaults.standard
    let imageData : NSData = UIImagePNGRepresentation(image)! as NSData
    defaults.set(imageData, forKey: "profileImage")
}

}

0

You can use the return statement

get{
   let defaults = UserDefaults.standard

   guard let imageData = defaults.object(forKey: "profileImage") as? NSData else{
     downloadAndSetProfileImage(completionHandler: { (profileImage) in

     return profileImage
     })
   }

   return UIImage(data: (imageData as! NSData) as Data)
}

If you get the data value type in your Handler termination, you should use the UIImage (data: profileImage) method

0
source

Source: https://habr.com/ru/post/1688329/


All Articles