Search code examples
iosswiftcore-datauiimageresolution

Automatic resolution for imported images, stored in Core Data


My application receives images inside a JSON file and stores them in a Core Data. For different resolutions it imports different images, like @2x and @3x. Is it possible to set an UIImage to get the data for different resolutions same way as application does automatically with @2x and @3x files? As an option I can take this data from the Core Data and write it as separate files with corresponding names, but I'm not sure that this is an optimal way.

Thank you for your advices in advance!


Solution

  • Depending on the device being used you can determine the scale. This will not change in the device.

    UIScreen.main.scale
    

    Based on the scale download only the images with the relevant resolution (example: 1x or 2x or 3x). Don't have to download them all and rename without @1x / @2x / @3x

    Personally I prefer to store the downloaded images in the cached folder instead of core data so that if it is low on disk space it can be removed automatically.

    Download images on demand (only when required to be displayed) asynchronously.