2017-10-28 87 views
4

是否有人可以請建議或指導,我如何返回在Location 1處獲得的ProfileImage,並將其返回到Location 2。將不勝感激您的幫助。我已經去了其他的SO問題,但沒有一個幫助我。我如何從完成處理程序中獲取一個值iOS Swift 3

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

回答

0
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

您可以使用回位控制器

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

如果收到數據的值類型在completionHandler,你應該使用方法的UIImage(數據:profileImage)

1

您不應該將異步任務放入您的getter中。相反,您可以使用可選的profileImage computed屬性僅用於從用戶默認值中獲取圖像,並創建另一個異步函數以獲取用戶配置文件,如果不是零,將從默認值返回圖像,否則會嘗試下載圖像。就像這樣:

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並在任務結束時傳遞結果。底線,你有一個情況需要一個異步任務,所以你需要某種完成處理程序,如我的例子中的一個。

相關問題