store file in NSDocumentDirectory of iPhone

7.4k views Asked by At

I am using a UIImagePickerController in my application.

After selecting an image by user,

image should be saved at application Documents Directory,

my Code is Give Below.

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
     [picker dismissModalViewControllerAnimated:YES];
     NSData *imgData = UIImagePNGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"]);
     UIImage *img = [[UIImage alloc] initWithData:imgData];
     stuImgView.image = img;
     [img release];

     //write image
     NSString *imageFilename = [NSString stringWithFormat:@"%i.jpg", currentStudent.stuNo];
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *path = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], imageFilename];
     UIImage *stuImg; 
     BOOL success; 
     NSFileManager *fm=[NSFileManager defaultManager];
     // how to store file at documents dir????
}

i dont know how to use file manager to store a file?

Help me Out.

Thanks in advance.

1

There are 1 answers

1
notnoop On BEST ANSWER

You can use writeToFile:atomically::

[imgData writeToFile:path atomically:NO];