文档目录存储捕获图像并将其加载到集合视图中的路径

时间:2016-10-03 07:35:02

标签: ios objective-c uicollectionview nsdocumentdirectory

我正在开发类似相机的应用程序。这是我拍摄照片的方法:

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

现在我需要将捕获的图像存储在NSDocumentdirectory路径中,并且我想在My collection视图中显示它们。

在此之前,我将图像保存在数组中并读取图像,然后将它们加载到集合视图中。请帮我这样做..我对这个NSDocument目录路径知之甚少。< / p>

3 个答案:

答案 0 :(得分:0)

文档文件夹的路径是

NSString* appDocumentsFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

要保存您必须使用的图像

[UIImagePNGRepresentation(image) writeToFile:[appDocumentsFolder stringByAppendingPathComponent:@"/imageName.png"]; atomically:YES];

要使用UICollectionView我建议您阅读相同的文章,例如:https://www.raywenderlich.com/136159/uicollectionview-tutorial-getting-started

答案 1 :(得分:0)

我修改了你的代码,将代码保存到Document目录中。

NB:我假设您在captureStillImageAsynchronouslyFromConnection方法中获取图片

我的代码:

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

        //Code to save image into Document directory

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"SampleImage.png"];
        [imageData writeToFile:savedImagePath atomically:NO];
    }
}];
}

希望它有所帮助...

快乐的编码。

答案 2 :(得分:0)

我给你详细解答

如果要将图像保存到文档目录

Android 6.0 (API level 23)

在上面的代码中,arrImg被保存在数组中。

然后你需要在代码下面写或打电话

for (UIImage *img in arrImg)
{
   int i=0;
   NSString *pathName =nil;
   NSString *fileName = [[self getCurrentDate]stringByAppendingString:[self getCurrentTime]];
   fileName =[file_name stringByAppendingPathExtension:@"jpeg"];
   NSArray *paths1 =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *basePath =([paths1 count] >i) ? [paths1 objectAtIndex:i] : nil;
   NSString *path = [basePath stringByAppendingPathComponent:@"Photo"];
   //Get Directory in FileManager
   NSFileManager *fileManager =[NSFileManager defaultManager];
   if ([fileManager fileExistsAtPath:path])
      return;
   [fileManager createDirectoryAtPath:path  withIntermediateDirectories:NO attributes:nil error:nil];
   pathName =[path stringByAppendingPathComponent:file_name];
   NSData *imgData =UIImageJPEGRepresentation(image, 0.4);
   [imgData writeToFile:pathName atomically:YES];
   NSMutableArray *arrImgpath = [[NSMutableArray alloc]init];
   [arrImgPath addObject:pathName];
}

从目录路径中获取图像时

-(NSString*)getCurrentTime
{
   //Get Current Time for saving Images
   NSString *path =nil;
   NSDateFormatter *timeFormatter =[[NSDateFormatter alloc]init];
   [timeFormatter setDateFormat:@"HH:mm:ss.SSS"];
   NSDate *now = [[NSDate alloc]init];

   NSString *str_time = [timeFormatter stringFromDate:now];
   NSString *curr_time;
   curr_time =[str_time stringByReplacingOccurrencesOfString:@"." withString:@""];

   path = [NSString stringWithFormat:@"%@",curr_time];

   return  path;
 }
 -(NSString*)getCurrentDate
 {
    NSString *today =nil;

    NSDateFormatter *dateFormatter1;
    dateFormatter1 =[[NSDateFormatter alloc]init];
    [dateFormatter1 setDateFormat:@"d MMM yyyy"];
    NSDate *now =[[NSDate alloc]init];
    NSLocale *usLocale =[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    [dateFormatter1 setLocale:usLocale];
    NSString *str_date =[dateFormatter1 stringFromDate:now];
    today=[NSString stringWithFormat:@"%@",str_date];
    return today;
 }