Reputation: 63
Here is the thing. At a point in my app, I want to show on a CollectionView the photos stored in Core Data (yes, I use External Storage). To get only the photos in my Entity I do the following:
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"SmallThing"];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"smallphoto", nil]];
self.photos = [[managedObjectContext executeFetchRequest:fetchRequest error:nil]mutableCopy];
That seemed to work fine, it returns the data for the photos and store it on the array. Then I created a method to convert it to UIImage so I could store on another array as following:
- (NSMutableArray *)convertDataToImage:(UIImage *)image {
for (NSData *data in self.photos) {
image = [[UIImage alloc] initWithData:data];
[self.images addObject:image];
}
return self.images;
}
When I get to the line image = [[UIImage alloc] initWithData:data
it crashes and I get the following error:
[16697:2367280] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSKnownKeysDictionary1 length]: unrecognized selector sent to instance 0x7fae0c117cc0'
I don't know what else I can do. Any ideas ?
Thanks in advance
Upvotes: 0
Views: 624
Reputation: 18363
You are explicitly telling the fetch request to return dictionaries with the line:
[fetchRequest setResultType:NSDictionaryResultType];
Which means the array returned by executeFetchRequest...
contains NSDictionary
objects, with key-value pairs corresponding to the properties you specified.
Your code in convertDataToImage
should be along the lines of:
for (NSDictionary *dictionary in self.photos) {
NSData *data = dictionary[@"smallphoto"];
...
}
If you specified a different property to fetch other than smallphoto
you would of course substitute that string there as appropriate.
Upvotes: 1