创建文件夹并添加多个图像

时间:2013-04-11 12:31:12

标签: iphone ios uiimage

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

提前致谢

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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.
        }
    }