I have set .heic
images in my xcassets
folder from my project to save some space. However I'm not able to load them through the UIImage(named:)
constructor. I'm always getting nil
, so the only way I have to load them is by specifying the URL. Do you have any idea why?
imageView.image = UIImage(named: "stone") // This returns nil.
Again, if I add them to the project as files and I access them through this method I created everything is fine (or using libraries like SDWebImage), but I believe I'm losing the power of app thinning as the images are hosted as files, so e.g. iPhone 7 will have both 2x
and 3x
resolutions when only 2x
is needed.
extension UIImage {
convenience init?(heicName: String, useScale: Bool = false) {
guard let resourcePath = Bundle.main.path(forResource: heicName.fileName(fileExtension: .heic, useScale: useScale), ofType: nil) else {
return nil
}
let url = URL(fileURLWithPath: resourcePath) as CFURL
guard let source = CGImageSourceCreateWithURL(url, nil),
let image = CGImageSourceCreateImageAtIndex(source, 0, nil) else {
return nil
}
self.init(cgImage: image)
}
}
So my question is, can anyone explain me the process to use HEIC as images hosted in the image assets folder and make it compatible with app thinning (just fetch the needed resources, i.e. 2x, 3x)?
UPDATE: After researching more I realised images are loaded from IB when I just set the image name on the image view. Do you know how this is internally working? which constructor is IB using to set the UIImage to the UIImageView?
UPDATE 2: I have submitted a radar to Apple: Radar