2016-10-04 2 views
0

イメージを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]; 
     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;} 

//を試してみましたパス

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

}

// Pそれは辞書

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

であれば、私は、同じ画像の複数の時間を取得した画像をrovideこれを行うにはhelpmeをしなさい..事前に

感謝を!

+0

いずれかが –

+0

おかげで、ravi.pをセルに割り当てる前に、私はシングルトンクラスから辞書内のすべての画像が欲しいし、コレクションビュー – Saraswathi

+0

でそれらを表示したいですあなたはすべての画像を含む辞書を持っていて、コレクションビューで1つずつ表示したいのですが。 – Saraswathi

答えて

0

あなたは本当に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]]]; 
    } 
}]; 

これは、特にインターネットからダウンロードした場合に、早い段階で画像を保存できることを意味します。

関連する問題