如何按日期和时间在文档文件夹中保存多个图像

时间:2015-05-22 17:36:44

标签: ios objective-c document

我想在文档文件夹中保存多个图像我在文档文件夹中保存图像成功但我的图像被覆盖我尝试按日期和时间保存图像 任何帮助都会欣赏,  我的代码是

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    _imageview.image=[info valueForKey:UIImagePickerControllerOriginalImage];
    NSData *pngData = UIImagePNGRepresentation(_imageview.image);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *Path = [documentsPath stringByAppendingPathComponent:@"image.png"];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"HH:mm:SS"];

    NSDate *now = [NSDate date]; 
    NSString *theDate = [dateFormat stringFromDate:now];

    [pngData writeToFile:Path atomically:YES];

1 个答案:

答案 0 :(得分:0)

将您的代码更改为:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    _imageview.image=[info valueForKey:UIImagePickerControllerOriginalImage];
    NSData *pngData = UIImagePNGRepresentation(_imageview.image);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"HH_mm_SS"];

    NSDate *now = [NSDate date]; 
    NSString *theDate = [dateFormat stringFromDate:now];
    NSString *fileName = [theDate stringByAppendingString:@".png"];

    NSString *Path = [documentsPath stringByAppendingPathComponent:fileName];

    [pngData writeToFile:Path atomically:YES];