UICollection视图崩溃

时间:2014-06-17 19:19:08

标签: ios core-data uicollectionview

我有一个摄像头视图,允许用户拍照并将其存储在名为" KTImage"的核心数据实体中。具有2个属性:图像(可转换)和时间戳。

以下是与保存到核心数据相关的代码:

- (IBAction)takePhotoButtonPressed:(id)sender {

    AVCaptureConnection *videoConnection = nil;

    for (AVCaptureConnection *connection in stillImageOutput.connections) {
        for (AVCaptureInputPort *port in [connection inputPorts]){
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                videoConnection =connection;
                break;
            }
        }
        if (videoConnection) {
            break;
        }
    }
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
        if (imageDataSampleBuffer != NULL) {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *photo = [UIImage imageWithData:imageData];
            self.imageView.image = photo;

            //store the image to core data
            NSManagedObjectContext *context = [KTCoreDateHelper managedObjectContext];
           self.image = [NSEntityDescription insertNewObjectForEntityForName:@"KTImage" inManagedObjectContext:context];

            self.image.image = photo;
            self.image.timestamp = [NSDate date];

            NSError *error = nil;
            if (![context save:&error]){
                //We have an error!
                NSLog(@"%@", error);
            }
        }
    }];

    AudioServicesPlaySystemSound(1108);    
    }

当我尝试查看集合视图时,应用程序崩溃 我是ios开发的新手,我很感激帮助弄清楚我做错了什么。

以下是集合视图控制器的.m文件:

import "KTCollectionViewController.h"
#import "KTImage.h"
#import "KTCoreDateHelper.h"
#import "KTCollectionViewCell.h"

@interface KTCollectionViewController ()

@end

@implementation KTCollectionViewController {
    NSMutableArray *images;
    NSMutableArray *imageDetails;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    images = [[NSMutableArray alloc]init];
    imageDetails = [[NSMutableArray alloc]init];

}

-(void)viewWillAppear:(BOOL)animated{

    //Fetch the images for the paging scroll view
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"KTImage"];
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES]];
    NSManagedObjectContext *context = [KTCoreDateHelper managedObjectContext];

    NSError *error = nil;

    NSArray *fetchedImages= [context executeFetchRequest:request error:&error];
    for(KTImage *photo in fetchedImages){
        [images addObject:photo.image];
        [imageDetails addObject:photo.timestamp];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -Collection View Methods

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return images.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KTCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
    cell.imageView.image = [images objectAtIndex:indexPath.row];
    cell.detailLabel = [imageDetails objectAtIndex:indexPath.row];

    return cell;

}
@end

我得到的错误:

'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

0 个答案:

没有答案