【发布时间】:2017-02-11 05:15:51
【问题描述】:
我正在开发相机应用程序。我需要将捕获的图像存储到 NSDocumentDirectory 并将该图像加载到 Collection 视图中。我只使用了数组。我对 NSDocument 目录了解不多。请任何人帮助我做到这一点。 这是我的代码,我将捕获图像保存到 nsdocument 目录路径中
-(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");
}
这是我的代码,我正在将 nsdocumnet 目录路径中的图像加载到集合视图中。
-(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;}
我已经尝试过,它正在获取图像并保存在 nsdocumentdirectory 路径中,但它只显示以前的图像。如果我再次运行它,它会显示最新的图像。请帮助我。检查此代码并提出任何想法
提前致谢!!!
【问题讨论】:
-
感谢@jecky,我已成功保存图片,但我不知道将图片加载到收藏视图中。请帮助我。
-
您可以从此代码中获取图像。 NSString * filePath = [[NSBundle mainBundle] pathForResource:
ofType: ]; cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageWithContentsOfFile: filePath]]; -
如果您保存了多个图像,那么这将对您有所帮助。 stackoverflow.com/questions/11293690/…
标签: objective-c uicollectionview nsdocumentdirectory