0

I am trying to the photo and video file path and storing into the sqlite. I am getting photo like this but I want know how i get the file path And store that into the sqlite And the same thing is i need with video alsoIs that possible to do that or is that any other idea or hints to solve that

-(IBAction) getPhoto:(id) sender {
    UIImagePickerController * UIPicker = [[UIImagePickerController alloc] init];
    UIPicker.delegate = self;



    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {


        UIPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:UIPicker animated:YES];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Error" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


    [picker dismissModalViewControllerAnimated:YES];
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

I thing people will help me out Thanks in advance

Shima
  • 144
  • 13

4 Answers4

2
 NSData *imageData = UIImagePNGRepresentation(sample);
NSData *myImage=[[NSData alloc]initWithData:imageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%d.png",i]];
[myImage writeToFile:fullPathToFile atomically:YES];
[myImage release];

Hope this code helps. Where i is an integer which can take any values 1,2,3.... increment i each time to avoid overwriting images.Now u can store many images without any overwrite problems.If u want to keep storing different images even after the application is closed you can use the NSUSerDefaults to store the last value of i.

NSString *myImage=[NSString stringWithFormat:@"Image%d",i];

After this increase the i value by 1. Then use the myImage string to store it in the sqlite database. Hope this helps.

Link1 Link2

Community
  • 1
  • 1
stack2012
  • 2,146
  • 2
  • 16
  • 23
  • it will store the photo in DocumentsDirectory what it is [NSString stringWithFormat:@"Image.png"] I didn't get you .Can you explain me more about the @"Image.png". – Shima Jul 20 '11 at 05:09
  • It is just the name of the image file. I am passing it to the stringByAppendingPathComponent after making it into a formatted string. I have edited the answer now.If u want u can ignore the format option and give the @"image.png" filename directly. Hope u got me now. – stack2012 Jul 20 '11 at 05:18
  • this is where i get the photo imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; "imageView.image" is my UIImageView *imageView .can i use the imageView replace with Image.png – Shima Jul 20 '11 at 05:37
  • No.... image.png is the file name. There u are naming the file b4 saving it to the documents folder.It must be a string. You cant pass an UIImageView object to it. – stack2012 Jul 20 '11 at 05:40
  • I get confused .Can you explain me more – Shima Jul 20 '11 at 05:48
  • There is nothing to get confused here. It s pretty simple. You are specifying the filename for your image b4 saving it to the docs folder. Or else just tell me clearly what u dont understand ? Let me try to explain it – stack2012 Jul 20 '11 at 05:53
  • I am doing like this it just simple to pick image and display on the imageView Which the the UIImageView Object [picker dismissModalViewControllerAnimated:YES]; imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; is that directly i use your code for storing the image in the DocumentsDirectory or I have do something else .if i am not wrong. – Shima Jul 20 '11 at 06:08
  • I thing it must like this NSData *imageData = UIImagePNGRepresentation(self.imageView.image); than after word your rest of code i thing so .IS that right . – Shima Jul 20 '11 at 06:27
  • But one problem is that it store only one image NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Image.png"]]; I think get overwrite again -again How can i solve that – Shima Jul 20 '11 at 07:30
  • Than how can we store that file path location into the sqlite database – Shima Jul 20 '11 at 08:43
  • @Shima let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1651/discussion-between-iphonefreak-and-shima) – stack2012 Jul 20 '11 at 09:05
  • I accept your answer but it not give any link for the video storing in db – Shima Jul 21 '11 at 11:34
  • Check the links I have posted below the answer.It might help – stack2012 Jul 21 '11 at 12:00
0

Check UIImagePickerControllerReferenceURL in the UIImagePickerControllerDelegate.

That will give the file path.

But I recommend store that image in some other place using NSFileManager and store that file path in your sqlite since the image might be deleted by some other ways.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
0

You need to store image into documents directory using NSFileManager. Search on google or stackoverflow you will find many tutorials. Then you store that path into sqlite that's it. I suggest you to use Unique identifier for each image you can find that code snippet too on goole or stackoverflow.

Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
0
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];


    NSString *filePath = [NSString stringWithFormat:@"%@/%@", docDirectory,@"ImageName.png"];
    awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10,60,60 )];
    awesomeView.image =[UIImage imageWithContentsOfFile:filePath];
    [mainView addSubview:awesomeView];
    [awesomeView release];

You can use this code to retrieve the image from documents folder and display it in the UIImageView. You can simply store the name of the file alone in sqlite . Using that u can replace the @"image.png" with the fileName retrieved from the database. Hope this helps.

stack2012
  • 2,146
  • 2
  • 16
  • 23
  • I do like this NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",camtitle.text]]; But when how can get this for displaying it – Shima Jul 20 '11 at 09:41