0

is it possible to save a image I have picked form the photos library to the Main Bundle of my app? I thought I read somewhere that you can't write to the main bundle of an app in Xcode. If this is not true and it is possible how would I go about doing it.

user1114881
  • 731
  • 1
  • 12
  • 25

1 Answers1

0
Use this code to save image from photo Library to Main Bundle

- (void)saveImage:(UIImage*)image withImageName:(NSString*)imageName {
    NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
    NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
}
User511
  • 1,456
  • 10
  • 28