[英]Convert array of NSData to array of UIImage
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). 在我的应用程序中的某个时刻,我想在CollectionView上显示存储在Core Data中的照片(是的,我使用外部存储)。 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: 然后,我创建了一种将其转换为UIImage的方法,以便可以将其存储在另一个数组中,如下所示:
- (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: 当我到达image = [[UIImage alloc] initWithData:data
行image = [[UIImage alloc] initWithData:data
它崩溃了,并且出现以下错误:
[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 提前致谢
You are explicitly telling the fetch request to return dictionaries with the line: 您明确地告诉fetch请求返回以下行的字典:
[fetchRequest setResultType:NSDictionaryResultType];
Which means the array returned by executeFetchRequest...
contains NSDictionary
objects, with key-value pairs corresponding to the properties you specified. 这意味着executeFetchRequest...
返回的数组包含NSDictionary
对象,其键值对对应于您指定的属性。
Your code in convertDataToImage
should be along the lines of: 您在convertDataToImage
代码应遵循以下原则:
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. 如果您指定了一个不同的属性来获取除smallphoto
以外的属性, smallphoto
您当然可以在其中适当地替换该字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.