【问题标题】:Saving and Retrieving Image to collection View将图像保存和检索到集合视图
【发布时间】:2017-02-12 09:46:28
【问题描述】:

我已将图像存储在单例类的 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];
        int x = arc4random() % 100;
        NSTimeInterval secondsSinceUnixEpoch = [[NSDate date]timeIntervalSince1970];

        self.UniqueImageName = [NSString stringWithFormat:@"%@_%d_%d",self.siteName,x,(int)secondsSinceUnixEpoch];

       [[SingletonImage singletonImage]SaveImageInNSdocumentAndCache:self.image withImageName:self.UniqueImageName];
         //reloading the collection view
        [self.collection_View reloadData];

我的单身班

      #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];


 //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];

}

//如果图片在字典中,则提供图片

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

        //Savedimage =
        NSLog(@"File exists at the path");

    }

    else

    {
        NSLog(@"Image doesnot exist");

    }
    return Savedimage;}

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {

   CollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];


//Loading images




UIImage *savedImage = [[SingletonImage singletonImage]ProvideImage:self.UniqueImageName];

Cell.image_View.image =savedImage;

return cell;}

我多次获得相同的图像。请帮我这样做..

提前致谢!!!

【问题讨论】:

  • 在分配给单元格之前将您的图像设为空图像
  • 谢谢,ravi.p,我想要字典中来自单例类的所有图像,并希望在集合视图中显示它们
  • 请任何人帮助我
  • 表示你有一本包含所有图片权限的字典,并且你想在collectionview中一个一个地显示它们
  • 是的,我的字典在单例类中,我不知道从中获取什么。

标签: objective-c uicollectionview nsdocumentdirectory


【解决方案1】:

你真的应该使用SDImageCache - 这很简单

#import <SDWebImage/UIImageView+WebCache.h>

将图像存储在内置缓存字典中:

[[SDImageCache sharedImageCache] storeImage:image forKey:@"image1"];

在任何时候都像这样检索图像:

SDImageCache *imageCache = [SDImageCache sharedImageCache];
NSString *key = @"image1"
[imageCache queryDiskCacheForKey:key done:^(UIImage *image , SDImageCacheType cacheType) {
     if (image)
     {
         view.image = image;
     }
     else
     {
         view.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:key]]];
     }
}];

这意味着您可以尽早存储图像 - 特别是当您从互联网下载图像时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 2017-01-28
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    相关资源
    最近更新 更多