My issue: I have reason to believe that initializing a UIImage
from Data
gums up my main thread a bit. It isn't terrible, takes a fraction of a second to complete, but it is noticeable as it makes my UI seem a bit glitchy.
What I'm doing: I have a UIPageViewController
. One of the UIViewController
s presented by said UIPageViewController
is making use of this UIImage(data: Data)
initializer in its viewDidLoad
method:
class PageViewController: UIPageViewController {
var pages: [UIViewController] = []
override func viewDidLoad() {
super.viewDidLoad()
loadPages()
}
...
func loadPages() {
let vc1 = storyboard!.instantiateViewController(withIdentifier: "VC1") as! ViewController1
let vc2 = storyboard!.instantiateViewController(withIdentifier: "VC2") as! ViewController2
let vc3 = storyboard!.instantiateViewController(withIdentifier: "VC3") as! ViewController3
pages.append(userProfilePreviewVC)
pages.append(meetVC)
pages.append(chatLogVC)
setViewControllers([pages[1]], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
}
}
extension PageViewController: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
switch currentIndex {
case 1:
return pages[0]
case 2:
return pages[1]
default:
return nil
}
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
switch currentIndex {
case 0:
return pages[1]
case 1:
return pages[2]
default:
return nil
}
}
}
Here is where the issue lies:
class ViewController1: UIViewController {
@IBOutlet var profilePicButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let userPhotos = UserDefaults.standard.array(forKey: "UserPhotos") as? [Data] ?? []
if userPhotos.count != 0 {
self.profilePicButton.setImage(UIImage(data: userPhotos.first ?? Data()) ?? UIImage(named: "profile")!, for: .normal)
} else {
self.profilePicButton.setImage(UIImage(named: "profile"), for: .normal)
}
}
}
My question: When swiping on my PageViewController
to the page that is displaying ViewController1
, the main thread seems to gum up as it takes a fraction of a second to actually load this ViewController1
. This is in contrast to other view controllers that are instantly ready to go when swiping to them. Similarly, when the user doesn't have any stored profile picture data, a placeholder image that is from my assets.xcassets
folder takes its place. There is no lag when presenting this image. My question is: should I use UIImage(data: Data)
on a background thread and update the button image when the UIImage
has initialized?