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

Asked 2 years ago, Updated 2 years ago, 80 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] = ["]

     letud=NSUserDefaults.standardUserDefaults()

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

     // 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);
        imageList.append(imageData!)
        ud.setObject(imageList, forKey: "imageList")
        ud.synchronize()
        }
    }

Output View Controller

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

swift2

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){

        cell!.memoImage.image=image
    }


2022-09-30 13:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.