1

I want to save displayed UIImageview image in SQL database. I am capturing the image using the camera/albums/library which are displayed in UIImageview. I want to save this image in my SQL database using URL.

Also I need to display this image in another view.

How can I take a path(URL) of my imageview image? How can I store this path(URL) & also store & display into another view?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Rain
  • 124
  • 1
  • 1
  • 9

2 Answers2

1

How to grab the UIImage and write to internal directory:

UIImageView *imageView = "[your image]";

// define your own path and storage for image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
documentPath = [documentPath stringByAppendingPathComponent:@"test.jpg"];
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 1.0);
[imageData writeToFile:documentPath atomically:YES];

How to get the NSURL/NSString for a file path in internal directory:

// specify fileURL as it is an internal file, don't use URLWithString:
NSURL *fileURL = [NSURL fileURLWithPath:documentDirectory];

// store as a string in database
NSString *fileURLString = fileURL.path;

Please take note that the NSURL for local file is different with normal NSURL.

// for normal URL
NSURL *webURL = [NSURL URLWithString:"http://www.google.com/"];
[webURL absoluteString]; // -> http://www.google.com/

// for local file path URL
NSURL *localURL = [NSURL fileURLWithPath:"/User/path/samplefile.ext"];
[localURL absoluteString]; // -> file://User/path/samplefile.ext
[localURL path]; // -> /User/path/samplefile.ext
morph85
  • 807
  • 10
  • 18
  • i am already display image in imageview.but i only need this display imageview path to store into sql & used for another view.i am confused in sql part. – Rain Jun 30 '13 at 14:06
  • I'm assuming you're using SQLite for this task, please check the tutorial here for query, insert or update: http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application. You only can store string type (for URL) in database and then when you're retrieving it try to convert it back to NSURL again. – morph85 Jul 01 '13 at 06:25
0

just save name of your image in database and store image in phone memory...it will be easy or this way try this line

UIImage *img = [UIImage imageWithContentsOfFile:(the file path)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
iAhmed
  • 6,556
  • 2
  • 25
  • 31