문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top