【问题标题】:Document Directory Path to store capture image and load them in collection view用于存储捕获图像并将其加载到集合视图中的文档目录路径
【发布时间】:2017-02-11 02:22:52
【问题描述】:

我正在开发像相机这样的应用程序。这是我拍照的方法:

-(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 路径中,并且我想在我的收藏视图中显示它们。

在此之前,我将图像保存在数组中并读取图像并将它们加载到集合视图中。请帮我这样做..我对这个NSDocument 目录路径了解不多。

【问题讨论】:

    标签: ios objective-c uicollectionview nsdocumentdirectory


    【解决方案1】:

    文档文件夹的路径是

    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

    【讨论】:

    • 谢谢,Pol 我已经使用数组保存和加载图像。但我不知道使用 nsdocument 目录,请帮我这样做,分享一些示例代码
    【解决方案2】:

    我已经修改了你的代码,它将图像保存到文档目录中。

    注意:这里我假设,您正在使用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];
        }
    }];
    }
    

    希望对你有帮助……

    编码愉快。

    【讨论】:

      【解决方案3】:

      我给你详细解答

      当您要将图像保存到文档目录时

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

      在上面的代码中,arrImg 将图像保存在数组中。

      那么你需要编写或调用下面的代码

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

      当你从目录路径获取图片时

      for(int j=0;j<arrImgPath.count;j++)
      {
        NSString *localPath=[NSString stringWithFormat:@"%@",[arrImgPath objectAtIndex:j]];
        NSURL *strPathURL=[NSURL fileURLWithPath:localPath];
        NSData *data = [NSData dataWithContentsOfURL:strPathURL];
        UIImage *img = [[UIImage alloc] initWithData:data];
        imageView.image = img;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-02-11
        • 1970-01-01
        • 2020-09-08
        • 1970-01-01
        • 2012-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多