Shima
Shima

Reputation: 144

Getting photo and video file path and storing into the sqlite

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

Upvotes: 0

Views: 1404

Answers (4)

stack2012
stack2012

Reputation: 2186

 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

Upvotes: 2

stack2012
stack2012

Reputation: 2186

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.

Upvotes: 0

Ilanchezhian
Ilanchezhian

Reputation: 17478

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.

Upvotes: 0

Rahul Vyas
Rahul Vyas

Reputation: 28740

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.

Upvotes: 0

Related Questions