0

I have a camera view that allows the user to take a photo and store it in a core data entity named "KTImage" with 2 attributes: image(transformable) and timestamp.

Here is the code relevant to saving to core data:

- (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);    
    }

When I try to view the collection view the app crashes.
I am new to ios development and I would appreciate some help with figuring out what I am doing wrong.

Here is the .m file for the collection view controller:

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

The error I am getting:

'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
Kaitis
  • 628
  • 9
  • 21
  • It would help if you tell us what the error is. – Calvedos Jun 17 '14 at 19:40
  • @Calvedos sorry about that see edit – Kaitis Jun 17 '14 at 19:46
  • It looks like you're directly saving a UIImage to the CoreData store. I may be wrong but I don't think that will work. See http://stackoverflow.com/questions/16685812/how-to-store-an-image-in-core-data for an alternative. – Calvedos Jun 17 '14 at 20:12
  • The error you're getting is probably happening when you call addObject on the images array in the viewWillAppear. The photo.image property is nil and you can't add nil to an NSMutable array. – Calvedos Jun 17 '14 at 20:16

0 Answers0