从核心数据加载UICollectionView

我正在开发一个iOS应用程序,并在某个时间点存储用户在Core Data中绘制的图像。 现在我想将所有录制的图像加载到UICollectionView中,以便用户可以选择一个并在社交网络上共享。 除了这最后一部分,我的应用程序中的所有东西都可以工作。 我跟着网上的各种教程,但我可以在UICollectionView上找到的所有例子都是使用来自Flickr或类似网站的图片。从核心数据加载UICollectionView

这是我在这一点上代码:

#import "LibraryViewController.h" (The name of the class we're in) 

#import "SocialViewController.h"

#import "CollectionViewCell.h"

static NSString *cellIdentifier = @"MemeCell";

@implementation LibraryViewController {

NSMutableArray *_objectChanges;

NSMutableArray *_sectionChanges;

}

#pragma mark - View Controller LifeCycle

- (void)awakeFromNib

{

[super awakeFromNib];

}

- (void)viewDidLoad

{

[super viewDidLoad];

_objectChanges = [NSMutableArray array];

_sectionChanges = [NSMutableArray array];

self.title = @"Meme collection";

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

if ([[segue identifier] isEqualToString:@"share"]) {

NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] lastObject];

NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];

SocialViewController *destViewController = segue.destinationViewController;

[destViewController setDetailItem:object];

}

}

#pragma mark - UICollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];

return [sectionInfo numberOfObjects];

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier

forIndexPath:indexPath];

NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];

[cell setImage:[UIImage imageWithData:[object valueForKey:@"image"]]];

return cell;

}

#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController

{

if (_fetchedResultsController != nil) {

return _fetchedResultsController;

}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

// Edit the entity name as appropriate.

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Meme" inManagedObjectContext:self.managedObjectContext];

[fetchRequest setEntity:entity];

[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];

aFetchedResultsController.delegate = self;

self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;

if (![self.fetchedResultsController performFetch:&error]) {

// Replace this implementation with code to handle the error appropriately.

// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

abort();

}

return _fetchedResultsController;

}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo

atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type

{

NSMutableDictionary *change = [NSMutableDictionary new];

switch(type) {

case NSFetchedResultsChangeInsert:

change[@(type)] = @(sectionIndex);

break;

case NSFetchedResultsChangeDelete:

change[@(type)] = @(sectionIndex);

break;

}

[_sectionChanges addObject:change];

}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject

atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type

newIndexPath:(NSIndexPath *)newIndexPath

{

NSMutableDictionary *change = [NSMutableDictionary new];

switch(type)

{

case NSFetchedResultsChangeInsert:

change[@(type)] = newIndexPath;

break;

case NSFetchedResultsChangeDelete:

change[@(type)] = indexPath;

break;

case NSFetchedResultsChangeUpdate:

change[@(type)] = indexPath;

break;

case NSFetchedResultsChangeMove:

change[@(type)] = @[indexPath, newIndexPath];

break;

}

[_objectChanges addObject:change];

}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

{

if ([_sectionChanges count] > 0)

{

[self.collectionView performBatchUpdates:^{

for (NSDictionary *change in _sectionChanges)

{

[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {

NSFetchedResultsChangeType type = [key unsignedIntegerValue];

switch (type)

{

case NSFetchedResultsChangeInsert:

[self.collectionView insertSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];

break;

case NSFetchedResultsChangeDelete:

[self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];

break;

case NSFetchedResultsChangeUpdate:

[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];

break;

}

}];

}

} completion:nil];

}

if ([_objectChanges count] > 0 && [_sectionChanges count] == 0)

{

[self.collectionView performBatchUpdates:^{

for (NSDictionary *change in _objectChanges)

{

[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {

NSFetchedResultsChangeType type = [key unsignedIntegerValue];

switch (type)

{

case NSFetchedResultsChangeInsert:

[self.collectionView insertItemsAtIndexPaths:@[obj]];

break;

case NSFetchedResultsChangeDelete:

[self.collectionView deleteItemsAtIndexPaths:@[obj]];

break;

case NSFetchedResultsChangeUpdate:

[self.collectionView reloadItemsAtIndexPaths:@[obj]];

break;

case NSFetchedResultsChangeMove:

[self.collectionView moveItemAtIndexPath:obj[0] toIndexPath:obj[1]];

break;

}

}];

}

} completion:nil];

}

[_sectionChanges removeAllObjects];

[_objectChanges removeAllObjects];

}

@end

当我运行,并尝试访问该视图控制器,应用程序崩溃与此错误消息: 2013年7月25日23:08:11.832 MemeGen [ 87259:C07] *终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因是:“+ entityForName:无不是合法的NSManagedObjectContext参数搜索实体名称‘梅梅’” *第一掷调用堆栈:

和它指向这行代码:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Meme" inManagedObjectContext:self.managedObjectContext]; 

显然self.managedObjectContext为零。 我应该如何分配它?当用户首次启动应用程序并且核心数据为空时,我应该如何管理它?禁止访问,只要没有图像?

否则,我知道如何将绘制图像存储在文件系统上。如果有人知道在UICollectionView中加载它的方法,它也可能是我可以接受的解决方案。

回答:

您将希望获得对您的ManagedObjectContext的引用。通常这会在应用程序委托中创建。在这种情况下,你会想这样的事情 -

NSManagedObjectContext *aManagedObjectContext = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext; 

如果你愿意,你可以做到这一点在你的viewDidLoad并将其分配给self.managedObjectContext。

回答:

除非您使用外部存储功能,否则我不建议将图像存储在核心数据中。大的二进制对象很少是你想要存储在核心数据之类的东西。当然,您可以将文件系统路径或URL存储在指向文件系统映像的核心数据对象上。

要使用带有UICollectionView的Core Data,请查看NSFetchedResultsController。 这里有一个example使用这两者来帮助你入门。

以上是 从核心数据加载UICollectionView 的全部内容, 来源链接: utcz.com/qa/260001.html

回到顶部