0

I am creating a camera app for iphone, and I am facing one issue.

I want to be able to give the picture I take, to be stored in the app's private directory and also to give picture a custom name to it (certainly via code). Eg I took a pic, I want to name the pic as mmddyyyyhhmmssXYZ and store thing image in myApp/images directory.

Please let me know if this is possible. And if it is possible, any suggestion or tutorial regarding would be very helpful.

Being new to iphone app development... I am actually clueless about this right now.

Thanks

Omarj
  • 1,151
  • 2
  • 16
  • 43
Zeeshan Rang
  • 19,375
  • 28
  • 72
  • 100

2 Answers2

1

Here is my code see Below.

-(void)copyImagesToCache:(NSString*)imageURl
{

    NSFileManager *filemanager=[NSFileManager defaultManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"MyIMages"];

            //now no need to use below single line of code.
//NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"image.png"];
//it just a example here , here you should make a source  path as you pic the images form the camera.

EDIT:While Taking pick in ImagePicker you should make a path . see below delegate of ImagePickerViewControllerDelegate.i have shown the way

    if(![filemanager contentsOfDirectoryAtPath:folderPath error:nil]){
        [filemanager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];

    }
    NSString *destPath = [folderPath stringByAppendingPathComponent:@"image1.png"];//here you should create the dynamic name so that you can distinguish all images.
    NSError *err;
  BOOL isCopeid= [filemanager copyItemAtPath:srcPath toPath:destPath error:&err];
  if(isCopeid)
    NSLog(@"Copied");
}


  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:     (UIImage *)image editingInfo:(NSDictionary *)editingInfo
 { 
  myIMage.image = image;
 //here call the method whcih copy the image to cache
 [self copyImagesToCache: [editingInfo objectForKey:UIImagePickerControllerReferenceURL]]];  
 [here is the link which shown the  editingInfo's keys ][1] 

//remaining code goes here as it is   
}

I hope it may help you...!!!!

Kamar Shad
  • 6,089
  • 1
  • 29
  • 56
0

Function to write your image to the app's private directory-

-(void)writeImagetoPath
 {

    UIImage *myImage = //This is the UIImage you obtained from the camera or the photo library.

    //This is the path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"xyz.png",i]];//You can give any name to the path.

    //Now convert the uiimage to data before writing to file.
    NSData *imgData = UIImagePNGRepresentation(myImage);
    [imgData writeToFile:documentsPath atomically:YES];

}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
Augustine
  • 1,714
  • 1
  • 17
  • 21