我正在开发一个类似的应用程序,从相机拍摄照片并将它们保存在字典数组和单例类中的nsdocument目录路径中。我在与每个单元格对应的集合视图中显示已保存的图片。 我试过了,但我得到的是,集合视图图像在每个单元格中显示相同的图像。
示例数组计数为2,对于2个单元格显示最新图片,即第二张图片。所以请帮助我。 代码如下所示......
CameraVC Class
//拍照时
-(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];
int x = arc4random() % 100;
NSTimeInterval secondsSinceUnixEpoch = [[NSDate date]timeIntervalSince1970];
ImageName = [NSString stringWithFormat:@"%@_%d_%d",self.siteName,x,(int)secondsSinceUnixEpoch];
[[SingletonImage singletonImage]SaveImageInNSdocumentAndCache:self.image withImageName:ImageName];
[self.collection_View reloadData];
}
}];}
我的收藏查看方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [[[SingletonImage singletonImage]arrayDict] count];}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
//Loading images in collection view cell
Cell.image_View.image = [[SingletonImage singletonImage]ProvideImage:ImageName];
return Cell;
}
这是我的单身人士课程
#import "SingletonImage.h"
@implementation SingletonImage
- (id)init
{
self = [super init];
if ( self )
{
self.arrayDict = [[NSMutableArray alloc] init];
self.imageDict = [[NSMutableDictionary alloc]init];
}
return self;}
+(instancetype)singletonImage
{
static SingletonImage *singletonImage;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singletonImage = [[SingletonImage alloc]init];
});
return singletonImage;}
//将图像保存到字典数组和nsdocument目录路径
-(void)SaveImageInNSdocumentAndCache:(UIImage *)image withImageName:(NSString *)str
{
//Saving image in array of dictionaries
[self.imageDict setObject:image forKey:str];
[self.arrayDict insertObject:self.imageDict atIndex:0];
NSLog(@" array of dictionaries%@",self.arrayDict);
//Saving image in nsdocumnet directory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSData *imageData = UIImagePNGRepresentation(image);
NSString *imageFolder = @"Photos";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyMMddHHmmss"];
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, str] contents:nil attributes:nil];
[imageData writeToFile:[NSString stringWithFormat:@"%@/%@", imagePath, str] atomically:YES];}
//提供图像(如果它在字典中,或者从nsdocument目录路径提供
) - (UIImage *)ProvideImage:(NSString *)text {
UIImage *Savedimage;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:text];
if ([self.imageDict objectForKey:text])
{
Savedimage = [self.imageDict objectForKey:text];
}
else if([[NSFileManager defaultManager]fileExistsAtPath:filePath])
{
Savedimage = [UIImage imageWithContentsOfFile:filePath];
NSLog(@"%@",Savedimage);
NSLog(@"File exists at the path"); }
else
{
NSLog(@"Image doesnot exist");
}
NSLog(@" the saved image is :%@",Savedimage);
return Savedimage;}
VC.h文件 这里我声明了nsstring
@interface CameraViewController : UIViewController<UICollectionViewDataSource ,UICollectionViewDelegate,UIImagePickerControllerDelegate>
{ NSString *ImageName;
}
和ImageName在单元格中用于索引路径方法的项目,以从singleton类获取图像。 我尝试过我所知道的。