2013-04-11 69 views
0

我正在创建一个iPhone应用程序,我需要创建单独的文件夹并在这些文件夹中添加图像并在Google驱动器上上传整个文件夹。我如何创建文件夹和图像。创建文件夹并添加多个图像

在此先感谢

+0

我想要做同样的事情,你能做到这一点吗?你是怎么做到的?我能够创建目录并在该文件内部,但无法一次获取文件夹的全部内容。我正在获取文件列表,但想要获取整个内容并在一次调用中发送到服务器。请提示。 – reetu 2017-04-06 03:22:56

回答

1

创建文件夹

 NSString *pngPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"/WB/%@",self.viewIndex]]; 

     // Check if the directory already exists 
     BOOL isDir;   
     BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:pngPath isDirectory:&isDir]; 
     if (exists) 
     { 
      if (!isDir) 
      { 
       NSError *error = nil; 
       [[NSFileManager defaultManager] removeItemAtPath:pngPath error:&error]; 
       // Directory does not exist so create it 
       [[NSFileManager defaultManager] createDirectoryAtPath:pngPath withIntermediateDirectories:YES attributes:nil error:nil]; 
      } 
     } 
     else 
     { 
      // Directory does not exist so create it 
      [[NSFileManager defaultManager] createDirectoryAtPath:pngPath withIntermediateDirectories:YES attributes:nil error:nil]; 
     }   

将图像添加到该文件夹​​

NSString *pngImagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"/WB/%@/%d.png",self.viewIndex,lineIndex]]; 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0); 
    [self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    self.curImage = UIGraphicsGetImageFromCurrentImageContext(); 
    [UIImagePNGRepresentation(self.curImage) writeToFile:pngImagePath atomically:YES]; 
    UIGraphicsEndImageContext(); 
+0

@“/ WB /%@”是文件夹名称? – arshad 2014-01-09 08:57:09

+0

WB是文件夹名称,里面的所有图像将在那里。 @arshad – 2014-01-09 08:58:32

0

试试这个:

//为了节省攻略目录

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"yyyy-MM-dd"]; 

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; 
[timeFormat setDateFormat:@"HH:mm:ss"]; 

NSDate *now = [[NSDate alloc] init]; 

NSString *theDate = [dateFormat stringFromDate:now]; 
NSString *theTime = [timeFormat stringFromDate:now]; 

NSString *strImageName=[NSString stringWithFormat:@"Documents/MyPhotos/Dt%@%@.png",theDate,theTime]; 
strImageName=[strImageName stringByReplacingOccurrencesOfString:@"-" withString:@""]; 
strImageName=[strImageName stringByReplacingOccurrencesOfString:@":" withString:@""]; 

[self createDocumentDirectory:@"MyPhotos"]; 
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:strImageName]; 
[UIImagePNGRepresentation(YourImageView.image) writeToFile:pngPath atomically:YES]; 





    -(void)createDocumentDirectory:(NSString*)pStrDirectoryName 
    { 
     NSString *dataPath = [self getDocumentDirectoryPath:pStrDirectoryName]; 

     if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:NULL]; 
    } 

    -(NSString*)getDocumentDirectoryPath:(NSString*)pStrPathName 
    { 
NSString *strPath = @""; 
     if(pStrPathName) 
      strPath = [[kAppDirectoryPath objectAtIndex:0] stringByAppendingPathComponent:pStrPathName]; 

     return strPath; 
    } 

    //Get Photo Onebyone 


    NSError *error = nil; 
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self getDocumentDirectoryPath:@"MyPhotos"] error:&error]; 

    if (!error) { 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"]; 
     NSArray *imagesOnly = [dirContents filteredArrayUsingPredicate:predicate]; 

     for (int i=0;i<[imagesOnly count]; i++) { 
      [arrSaveImage addObject:[imagesOnly objectAtIndex:i]];//Save in your array. 
     } 
    } 
相关问题