0

I have an array of UIImages, which is dynamic. So the user can add photos to this array. My question is how can I save this array, because I tried some things and none of them seem to work for me. I would welcome suggestions how to accomplish this task.

Thank you.

7
  • I tried core data, but i don't know how to save a array. Only single images... Commented Aug 5, 2016 at 0:42
  • 1
    Does it have to be an array of uiimages? Because you can create an array based on what you have searched for with in core data. Say for example you have an entity with attribute type of data (later used as uiimage) and an attribute of "favorite" which is a boolean indicating a user has favorited the image. You can use a predicate to sort for favorited entities and put those into an array using NSFetchedResultsController. If you can provide more detail about the situation we can be more helpful. Commented Aug 5, 2016 at 0:46
  • ok, so the user can make photos with the camera. Commented Aug 5, 2016 at 0:53
  • The photos are saved in an array called "images". i want to save those images when the app close and get them back to images when the app starts Commented Aug 5, 2016 at 0:54
  • I just don't get how i can save more then one image, without building hundreds of variables in the coreData. Commented Aug 5, 2016 at 0:55

1 Answer 1

1

If you want to use Core Data, I think the simplest way to save an array of images is to save them when a new image is added or an image is removed.

The Core Data data model is very simple. You can just add an entity called Image or whatever makes sense in your context. Add an image property to the entity. Set the property's type to "Data". Generate the NSManagedObject subclasses and the model is done.

Now, how and when do you need to save the images? I think you should insert an image to the Core Data context only when the user creates a new image. And you should remove an object from the Core Data context when the user removes an image. Because if the user does nothing the images in a session of your app, it's not necessary to save the images again.

To save a new image,

// I assume you have already stored the new image that the user added in a UIImage variable named imageThatTheUserAdded
let context = ... // get the core data context here
let entity = NSEntityDescription.entityForName(...) // I think you can do this yourself
let newImage = Image(entity: entity, insertIntoManagedObjectContext: context)
newImage.image = UIImageJPEGRepresentation(imageThatTheUserAdded, 1)
do {
    try context.save()
} catch let error as NSError {
    print(error)
}

And I think you know how to remove an image from Core Data, right?

When the VC that needs to display the images is presented, you execute an NSFetchRequest and fetch all the images saved as [AnyObject] and cast each element to Image. Then, use the init(data:) initializer to turn the data to UIImages.

EDIT:

Here I'll show you how to get the images back as [UIImage]:

let entity = NSEntityDescription.entityForName("Image", inManagedObjectContext: dataContext)
let request = NSFetchRequest()
request.entity = entity
let fetched = try? dataContext.executeFetchRequest(request)
if fetched != nil {
    let images = fetched!.map { UIImage(data: ($0 as! Image).image) }
    // now "images" is the array of UIImage. use it wisely.
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your Solution, but i didnt understand How to get the Images back. I didnt worked with Core-Data before. Maybe you have a Little sourcecode for me. Thank you.
Could i ask you were you get the DataContext method ? Thank you
It should be let context = (UIApplication.sharedApplication().delegate! as! AppDelegate).managedObjectContext @PaulHeinemeyer
Thank you, i just got one more problem i think. In the AppDelegate Class i append the elements of images to my array and when a photo comes from the camera i save it and i append it to my array, but then all appended saved images from the beginning are away. Maybe you got an idea for that problem. Thanks for your massive help :)
When you append the new images to the array, you should also insert it to the data context. Just use the first code snippet. @PaulHeinemeyer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.