Search code examples
iosuiimageuiimagepickercontrollernsdatansfilemanager

How to save uiimage in a directory on a given nsurl?


I have fetched UIImage from photos library in ipad using UIImagePickerController. Now I want to save that image in a directory that is associated with my app and contains already added images. I have also got the url of the directory.

For example : file://localhost/Users/administrator/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/31816EF9-AE1E-40DF-93C4-C67A82E4B85B/Documents/7884/Images/

How can I save uiimage in the specified directory?


Solution

  • Use this code to save it into your Document Directory.

    NSData *pngData = UIImagePNGRepresentation(image);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; 
    [pngData writeToFile:filePath atomically:YES];