Core data with images - ios

I'm new to iOS app development. My first question is it possible to create a core data entity that will have both text and images included? so attributes will be: Name, Price, Detail and Image. if so, how can I preload the data into the database?

you have to change image into (pngData) them save it and then when reading the image change (pngData) to image
and in core Data model make the image attribute of type binary Data
that source will help
https://blog.devgenius.io/saving-images-in-coredata-8739690d0520

Related

List of images with their properties

My problem:
In my app there are a lot of images. I have a list of categories which user can choose for filtering all images by chosen category. User can't delete/edit these images, and can't add smth in it, so I guess I don't need the Core Data.
So my question is:
How and where in my project should I store and manage images' names with their properties, so I could use this list with files' names and their properties from any ViewController?
Finally, if you didn't get it: It should look like this:
1.Name: "imagename.jpg", Category: "Somecategory")
2.Name: "imagename2.jpg", Category: "Anothercategory")
Thank you.
If you want to store your images under NSDocuments with a susbfolder , I think you can do it without using database and Core Data .
Let's think a scenerio for you based on Model View Controller(MVC) structure:
You want to store images with an special properties like NSString *categories and NSString *filePath under NSFileManager with a subfolder . Encode your object and convert it to NSData. You need an Model class(it's subclass of NSObject)
that keeps your properties. You need to learn how to save a file to your Documents path. This blog is very good but it's Objective C. This is your Model and your image object.
Create a View that shows your images UICollectionView (just an example) and collect them with your image object. Modify it how you want!
Create your Controller class and do all actions here. Like saving image to path, getting image from path, all UIButton actions etc... Add your View to this class,decode your NSData to your image object show it.
This is the logic of an example, you can do it according to your necessities.
The way you should following is saving your image data to NSDocuments , search it. I advice you to learn MVC Stucture
Hope this give you an idea to start.

RestKit CoreData and UIImage

I'm using Rest Kit with Core Data, one of the Core Data entities has an attribute 'image' that has a binary type.
I'm still in mockup stage so the image is populated with this code:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://lorempixel.com/60/60/people"]]];
entry.image = UIImagePNGRepresentation(image);
Another tab has a collection view that uses fetchedResultsController.
After creating a new entity, if I only save the context the image works fine.
But if I push the entity to the web server using 'postObject:' the image is corrupted when it comes back from the server. I've confirmed the server receives the same string representation of the image "<2f396a2f 34414151 536b5a4a 52674142 ... 6a6e502f 32513d3d>" and stores it directly into a MySQL column of type long blob and at all points the string representation is the same.
But when the collection view is populated using a server call via RestKit the entities image is invalid. I'm think the issue is the data is being converted into the data representation of the description of the data.
Does anyone have a working example with images. The only thing I can think of is that I need to add a custom transformation, but the documentation and examples are lacking as far as how to actually implement one.
RestKit is storing the plain NSData for the image in Core Data - it has no idea what else you might want to do with it. Generally you don't want to manage images directly in Core Data or using RestKit.
Generally, store the path of the image in Core Data and the file on disk. Download them asynchronously (from the URL's which would also be in Core Data).
For uploading, you could make RestKit upload the data, but you probably actually want to file upload or convert to base64. You will need to write some code for this (which you could have RestKit pick up by using the key of the method name that returns the appropriate data). A similar process will work for mapping the data in.
RestKit data transformers are hard to make work in this situation as you are converting between data and strings and they are too general to be able to intercept accurately.

Storing a persistent array of UIImages - CoreData or on disk?

In my app I will have an array of up to 50 images that people can maintain. They can choose to create new or delete existing images. Each image will have a few things associated with them, like a rating for example.
My question is how I should go about storing them. Should I create a CoreData entity called "Image" and store them that way? Should I set up a UIView subclass that conforms to NSCoding and just encode and decode the array and store it on the device? Is there another way I should consider? Thanks for any suggestions.
You can create an entity that represents the image with its information, and use core data's external storage property for entity's attribute. This way, you get the convenience of core data without actually storing the images on the persistent store.
I had to make a similar decision recently and I decided to store the image in CoreData. I have a managed object called photo with a Binary Data field called image:
photo.image = UIImagePNGRepresentation(imageFile); // <- imageFile is a UIImage
And I recreate the image using:
[UIImage imageWithData:self.image];
One immediate advantage is that the images are deleted automatically with the object and there's no extra overhead in retrieving the image if you've already queried for the record.
Core Data is probably overkill for what you want to do. Choose some key value pairs for descriptive information about the image. One key will be "path" and the value will be the path to the image file (or just its name). You can serialize array (or set) of dictionaries.
When your app starts up, read in the serialized array of dictionaries. Every time something changes, serialize and save the information. Write a short audit routine to insure that there is a one - to - one correspondence between dictionaries and images on file, and if one or the other is missing delete the other (will handle situations were you crash before getting to update something or the other).
Later on you can add more attributes to the dictionaries if you want, or even remove some.
[PS: I did this in a shipping app for one version, when the information needed to become relational I switched to Core Data.]

Core data. How to organize relations beetween two resources. (one has urls of images used by another)

I am developing a Forthsquare-like app for iOS. Using Core Data I am preparing the schemes for two resources: Place and Category. Each place belongs to category. Each category has *image_url* field containing a link to an image (hotel, restaurant and so on), so that every place being displayed on map uses its category image as an annotation marker.
The question is what approach to choose and how to organize the relations beetween these resources to have categories images (fetched through the *image_url* field) being cached, so they could be reused by all the places having the same category without a need to retrieve category image every time when every given place is loaded?
Being new to iOS & Core Data stuff I want to know if there are any good practices for accomplishing this kind of tasks.
Thanks.
What I do in my app in similar circumstances is add some properties to the Category (note you can save the image data directly in core data if the image is "small", or save it on disk and a filePath to the image if its "large"):
bool fetchingImage
NSInteger categoryID
url (to fetch from)
imageData or filePath
When the category is created and the url assigned, you can fetch the image (in the background) and set the fetchinImage flag to yes. Or you can wait til the first object requests the image, in which case you return nil but initiate the fetch.
When the image finally arrives, and you set it in this entity, you send a notification on the main thread that says categorID now has an image. Anyone listening for that can then request the image and make it viewable.

Image handling rails + iOS Core Data

I am developing an iOS app that will have a number of images. These images are associated with Show object and Band objects. I am fairly new to iOS development
On the server, the Shows and Bands are associated with a list of images. Currently I am storing the images with the following info:
height:integer width:integer imageType:string imageData:binary
First question is: should there be more?
Secondly, I am persisting the Show and Band objects using Core Data. I do not want to persist the images because I would quickly run out of memory. I will store them in the cache directory. My second question is: how should the Show and Band objects keep track of the images? Should I have a Image objects in the model with a to many relationship with Shows and Bands. But these Image objects would perhaps only contain height, width, imageType and a path to where the cached image should be. My idea is that if it is not found in the cache directory, it gets the imageData from the server.
What is the right way to do this?
UPDATE
I also plan on pinging the server with HEAD to check if the imageData has been updated before getting the cached version.
you can actually just store the images directly with Core Data and not have to mess with documents at all.
This has been answered in other places but I'll put the code here anyway:
To save:
NSData *imageData = UIImagePNGRepresentation(yourUIImage);
[newManagedObject setValue:imageData forKey:#"image"];
and to load:
NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:#"image"]];
[[newCustomer yourImageView] setImage:image];
The way you should model your data depends on what kind of relationship the images will have with bands/shows.
Do shows and bands each relate to multiple images? Can a show have images that are related to multiple bands?
Ultimately you may want to have an Image Core Data entity that will have a one-one or one-many relationship with bands/shows depending on your answers to these questions. You will also want to add the inverse relationship so that for example when you look up a show, you can also access the set of images associated with it.
It might be easier to help you if you provide more detail about the relationships between your objects.

Resources