Store file in iPhone NSDocumentDirectory

I am using UIImagePickerController in my application.

After user select image

The image must be saved in the applicationโ€™s document directory,

my code is 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 do not know how to use the file manager to store the file?

Help me.

Thanks in advance.

+3
source share
1 answer

You can use writeToFile:atomically::

[imgData writeToFile:path atomically:NO];
+3
source

Source: https://habr.com/ru/post/1714800/


All Articles