I want to store the images in an array, save the array in NSUserDefaults, and take the images out of it

Asked 2 months ago, Updated 2 months ago, 3 views

We register images and sentences with the input side view controller and create an application like Notepad with photos that displays them in the table view of the output side view controller.

I want to put the images displayed in ImageView in an array, save the image array in NSUserDefaults, and display the images taken out of the array in ImageView in the cells of another view controller's table view, but I'm stuck.

1. Store images in an array
2. Save the array to NSUserDefaults
3. Remove the image from it and display it sequentially on the cell of the table view
I don't know which stage the mistake is occurring.

The build is done without any problems.
Thank you for your cooperation.

Input side view controller

// Create an array
     varimageList: [AnyObject] = ["]


     // Array in NSUserDefaults
     ud.setObject(imageList, forKey: "imageList")

     // Place ImageView images in an array and synchronize the array to NSUserDefaults
     if var imageList: [AnyObject] = ud.objectForKey("imageList")!as![AnyObject]{

     iflet image=displayImage.image{
        let imageData=UIImageJPEGRepresentation(image,1);
        ud.setObject(imageList, forKey: "imageList")

Output View Controller

// Get image data
    let imageList=ud.arrayForKey("imageList")
    iflet image: [UIImage] = imageList as?[UIImage]{
        cell!.displayImage.image=image [indexPath.row]


2022-09-30 13:47

2 Answers

Why don't you check per NSKeyedArchiveObject? You can archive it to NSData

2022-09-30 13:47

I solved myself.
By setting the output side view controller to the code below, I was able to take it out and display it in order.
Thank you Nagonsoftware and ucb.

 iflet imageData=ud.arrayForKey("imageList")![indexPath.row] as? NSData, image=UIImage(data:imageData){


2022-09-30 13:47

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.