2013-04-11 58 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. 
     } 
    } 
相關問題