I am working on camera app .I need to store the captured image into NSDocumentDirectory and load that images into Collection view. I did using array only .I don't have much knowledge about the NSDocument directory .Please anyone help me to do this. This is my code ,i am saving the capture image into nsdocument directory path
-(IBAction)takephoto:(id)sender
{
tapCount += 1;
AVCaptureConnection *videoConnection = nil;
for(AVCaptureConnection *connection in StillImageOutput.connections)
{
for(AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo]){
videoConnection =connection;
break;
}
}
}
[StillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error){
if (imageDataSampleBuffer!=NULL) {
NSData *imageData =[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
self.image = [ UIImage imageWithData:imageData];
//Saving Image in Document directory Path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSData *imageData1 = UIImagePNGRepresentation(self.image);
NSString *imageFolder = @"Photos";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyMMddHHmmss"];
NSDate *now = [NSDate date];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *myUniqueName = [NSString stringWithFormat:@"Photo-%@.png",theDate];
NSString *imagePath = [NSString stringWithFormat:@"%@/%@",documentDirectory,imageFolder];
BOOL isDir;
NSFileManager *fileManager= [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:imagePath isDirectory:&isDir])
if(![fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:NULL])
NSLog(@"Error: folder creation failed %@", documentDirectory);
[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/%@", imagePath, myUniqueName] contents:nil attributes:nil];
[imageData writeToFile:[NSString stringWithFormat:@"%@/%@", imagePath, myUniqueName] atomically:YES];
[self.collection_View reloadData];
}
}];
inViewDidLoad
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentDirectory, @"Photos"]; // image path is same as above
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fullPath error:nil];
self.fileList=[[NSMutableArray alloc]init];
self.fileName=[[NSMutableArray alloc]init];
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
for (NSString *filename in dirContents) {
NSString *fileExt = [filename pathExtension];
if ([fileExt isEqualToString:@"png"]) {
NSString *fullPth = [fullPath stringByAppendingPathComponent:filename];
[self.fileList addObject:fullPth];
[self.fileName addObject:filename];
NSLog(@"File name : %@ ",filename);
NSLog(@"fullPth : %@ ",fullPth);
}
}
} else {
NSLog(@"No Files");
}
Here is my code , i am loading images from the nsdocumnet directory path into collection view .
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
//Loading images from path
//Cell.image_View.image = nil;
NSString *filePath = [self.fileList objectAtIndex:indexPath.row];
UIImage *image =[UIImage imageWithContentsOfFile:filePath];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (image) {
Cell.image_View.image = image;
// [activityIndicator stopAnimating];
//activityIndicator.hidden = YES;
self.collection_View.hidden = NO;
} else {
Cell.image_View.image = [UIImage imageNamed:@"tours-walk.png"];
}
});
});
Cell.layer.shouldRasterize = true;
Cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
return Cell;}
I have tried something ,it is taking the image and saving in nsdocumentdirectory path but it showing the previous images only.if i run it again it is showing the latest image .Please help me.check this code and suggest any idea
Thanks in advance !!!