-1

i want to save an image with new name every time in xcode using objective c. how to do that? i want to add a time string in the name, and i have stored time string in a variable. how to concatenate that variable with the name. code which i am using is as---

    NSDate *currDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
   [dateFormatter setDateFormat:@"dd.MM.YY HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    NSLog(@"%@",dateString);`

    NSData *myImageData = UIImageJPEGRepresentation(image, 1.0);
    [fileManager createFileAtPath:@"/Users/name/Desktop/myimage.jpg" contents:myImageData attributes:nil];
Volker
  • 4,530
  • 1
  • 20
  • 30
Ravi
  • 41
  • 4

1 Answers1

0

You already have it 95% complete. You don't want concatenation, as such, as you want to preserve the .jpg file extension, so use [NSString stringWithFormat:] to create the filename:

NSString *filename = [NSString stringWithFormat:@"/Users/name/Desktop/myimage-%@".jpg", dateString];
[fileManager createFileAtPath:filename contents:myImageData attributes:nil];

Hints:

  1. Don't hardcode the path like that; get the user's Documents folder using NSSearchPathForDirectoriesInDomains.
  2. You probably don't want a space in the formatted date string.
trojanfoe
  • 116,099
  • 18
  • 197
  • 233