save data image in array in swift

To save an image data into an array in Swift, you can use the following code:

main.swift
// assume `image` is your UIImage instance
let imageData = image.pngData() // or image.jpegData(compressionQuality: 1.0) if you want JPEG data
var imageArray = [Data]() // initialize an array to store the image data

if let imageData = imageData {
    imageArray.append(imageData) // add the image data to the array
}
318 chars
8 lines

This code first converts the UIImage instance into Data using the pngData() method. Then, it initializes an empty array to store the image data. Finally, it checks if the image data is not nil, and adds it to the array using the append() method.

You can add as many images as you want to the array by calling this code for each image.

related categories

gistlibby LogSnag