2016-10-03 47 views
0

我正在开发像相机这样的应用程序。这是我拍摄照片的方法:文档目录存储捕获图像并将其加载到收集视图中的路径

-(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目录路径没有太多了解。

回答

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

+0

谢谢,波尔我已经做了保存,加载图像使用阵列。但我不不知道要使用nsdocument目录,请帮助我做到这一点,为此分享一些示例代码 – Siva

0

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

注:在这里,我认为,你所得到的图像中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]; 
    } 
}]; 
} 

希望它可以帮助...

编码愉快。

0

我给你详细的解答

当要保存图像到上面的代码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]; 
} 

数组保存imges。

然后,你需要写信或致电下面的代码

-(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; 
} 
相关问题