0

I am working on an application that needs its user to upload a photo for him, using iphone, how can i implement this : when click on the image in my personal information page, the gallery is opend to choose a photo. when the photo is chosen it must appear instead of the previous one (at first it is the default pic). Notice that, the path of this photo or the photo itself(i do not know) must be stored in the DataBase for the next login. Moreover, this photo must be transferred to the server in order to be shown on the website when the same user login on the web.

I did it using the following code, i can choose a photo but when i save the path i could not load the pic again. it is a white box !! there is no photo !!

-(void) pickPhoto
{
    UIImagePickerController *picker ;
    picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    else
    {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
    [self presentViewController:picker animated:YES completion:nil];
}

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

UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];

if(!img)
    img = [info objectForKey:UIImagePickerControllerOriginalImage];


NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath =  [docDirPath stringByAppendingPathComponent:@"myImage.png"];
NSLog (@"File Path = %@", filePath);


UIButton *btn = (UIButton *)[self.tableView viewWithTag:2013];
[btn setBackgroundImage:img forState:UIControlStateNormal];

database= [[connectToDB alloc] init];
[database setDelegate:self];
[database saveUserPic:filePath ForUser:user.userId];
[self dismissViewControllerAnimated:YES completion:nil];


}

now to load the pic :

UIImage *img;
if(pic != nil)
     img=[UIImage imageWithContentsOfFile:pic];
else
     img= [UIImage imageNamed:@"business_userL.png"];

Any Help will be appreciated.!

etab
  • 199
  • 1
  • 14
  • `UIImagePickerController` should help. What's your code for now? – Larme Nov 17 '13 at 19:27
  • All your trying is to access the asset library of your iPhone. There are two way to achieve this: 1. Using UIImagePickerController, that is the part of a standard UIKit. 2. Using AssetLibrary.framework, that need to explicitly added to your project. Specifically, if you are looking for a simple and system provided interface for your task, UIImagePickerController should be your choice. On the other hand if you need total control and customizable interface, I would recommend you to use AssetLibrary.framework. Please do some some research on your own to make a decision. – Ayan Sengupta Nov 17 '13 at 19:31
  • I found that you have generated a path expression in your `filePath` variable and saving that to database. But I did not find any code to save the actual image in actual `filePath` location. Have you missed some lines of code? – Ayan Sengupta Nov 17 '13 at 20:11
  • no, what i tried is to save the path of the image not the image itself. but even so i can not load the image from its path ! for sending the photo using web service do i have to save the actual pic ? – etab Nov 17 '13 at 20:26
  • @etab : Yes, it is obvious that you have saved your path. But to load the image from the path you still need the image to be physically available in that path and that means you need to save the image at that path. I think you are just missing that. – Ayan Sengupta Nov 17 '13 at 21:23

1 Answers1

1

UIImagePickerControllerReferenceURL will return reference to local storage of your selected image.

NSURL* localUrl = (NSURL *)[info valueForKey:UIImagePickerControllerReferenceURL];

Now you can save this url in your database.

To retrieve the image : display image from URL retrieved from ALAsset in iPhone

Community
  • 1
  • 1
Maulik
  • 19,348
  • 14
  • 82
  • 137