Junior — Middle
Kuidas erinevad piltide salvestamise viisid iOS rakendustes UIImage ja Data objektidena?
sobes.tech AI
Vastus AI-lt
Image and Data are two different ways to store images in iOS:
-
UIImage is an object that contains the image in a format ready for display on the screen. It stores already parsed image data and provides convenient methods for working with it (e.g., scaling, rotation).
-
Data are the raw bytes of the image, for example, in PNG or JPEG format. This is how the image is stored on disk or transmitted over the network.
Usage example:
// Save UIImage as Data for writing to a file
if let image = UIImage(named: "photo"),
let imageData = image.pngData() {
// imageData can be saved to a file or sent over the network
}
// Create UIImage from Data
if let data = try? Data(contentsOf: imageURL),
let image = UIImage(data: data) {
// image is ready for display
}
Thus, UIImage is convenient for working with images in the UI, and Data for storage and transmission.