12

I'm working on an application that is almost the same as the sample codes i found here. But the method i want to do is different from the sample codes.
Link: PhotoLocations

The first screen will have an ImageView and 2 buttons (Choose Photo, Confirm). When the 'Choose Photo' button is tapped, it will navigate to another screen which retrieves photos from my iPad's photo gallery. When a photo is chosen, it will dismiss the current screen and return to the first screen displaying the chosen photo on the ImageView.

When the 'Confirm' button is tapped, the photo will be stored into my application's project (e.g. /resources/images/photo.jpg).

May i know how can i do this?

3
  • in iPhone you have to use UIImagePickerController to retrieve images from the gallery and you can have them into ur app by saving it in NSDocumentsDirectory
    – stack2012
    Sep 30, 2011 at 4:09
  • Yes thank you, but i would prefer to have some sample codes so that i can refer.
    – Lloydworth
    Sep 30, 2011 at 4:11
  • 2
    google "UIImagePickerController reference". Go to Apple's website.
    – Akshay
    Sep 30, 2011 at 4:23

3 Answers 3

32

This will take you to the image gallery and you can select the image.

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.delegate = self;
imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];

this will help you select the image

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{
    // Dismiss the image selection, hide the picker and

    //show the image view with the picked image

    [picker dismissViewControllerAnimated:YES completion:nil];
    //UIImage *newImage = image;
}

And then you can store this image to the documents directory...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];

For clicking the image yourself use this

- (IBAction) takePhoto:(id) sender
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    imagePickerController.delegate = self;
    imagePickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;

    [self presentModalViewController:imagePickerController animated:YES];
}
5
  • May i know how can i store the image from the ImageView to the documents directory?
    – Lloydworth
    Sep 30, 2011 at 4:59
  • I just thought of an extra function. In the first screen, i will change the 'Choose Photo' button to 'Browse from photo gallery' and also add a new button 'Take a new photo'. By tapping on that button, my application will launch the camera and once the user takes a picture, it will return to the first screen showing the picture on the ImageView which can be saved into my documents directory too.
    – Lloydworth
    Sep 30, 2011 at 6:35
  • The last part .. which says "For clicking the image yourself use this" Sep 30, 2011 at 7:09
  • The storing part isn't working for me. I tried putting another imageview called 'ImageView2' beside. Then after i tapped the 'Confirm' button, i added in extra codes: ImageView2.image = [UIImage imageNamed:@"savedImage.png"]; but it doesn't display the image! :(
    – Lloydworth
    Sep 30, 2011 at 8:21
  • [UIImage imageNamed:@"savedImage.png"]; this works only for images stored in the app bundle.. for accessing the image from the documents directory you need the following... NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES ); NSString *docDirectory = [sysPaths objectAtIndex:0]; NSString *filePath = [NSString stringWithFormat:@"%@/Thumbnail-small.jpg", docDirectory]; background.image = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease]; Sep 30, 2011 at 9:03
3
UIImagePickerController *cardPicker = [[UIImagePickerController alloc]init];
cardPicker.allowsEditing=YES;
cardPicker.delegate=self;
cardPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:appDelegate.cardPicker animated:YES];
[cardPicker release];

And this delegate method will return the image u select from the album

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    noImage.image=img;
}
2
  • where noImage is the UIImageView in which i am displaying the image picked from the album...
    – stack2012
    Sep 30, 2011 at 4:36
  • May i know how can i store the noImage to the documents directory?
    – Lloydworth
    Sep 30, 2011 at 5:00
0

- (IBAction)UploadPhoto:(id)sender {
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please Select Your Option"delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Gallery",nil];
    
    [actionSheet showInView:self.view];
    
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    
    NSIndexPath *clickedButtonPath = [jobstable indexPathForCell:clickedCell];
    
    isSectionIndex = clickedButtonPath.section;
    isRowIndex     = clickedButtonPath.row;
    
}


-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    
    
    NSLog(@"From didDismissWithButtonIndex - Selected Option: %@", [actionSheet buttonTitleAtIndex:buttonIndex]);
    
    NSString*image=[actionSheet buttonTitleAtIndex:buttonIndex];
    
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Camera"]) {
        
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Device Camera Is Not Working" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
            return;
        }
        else{
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            [self presentViewController:picker animated:YES completion:NULL];
            
        }
    }
    
    else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Gallery"]){
        
        
        UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
        pickerView.allowsEditing = YES;
        pickerView.delegate = self;
        [pickerView setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
        [self presentViewController:pickerView animated:YES completion:nil];
        
    }
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex];
        
        UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath];
        
        UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19];
        
        tableIMAGE.image=orginalImage;
        
  answersARRAY[indexPath.row] = [NSString stringWithFormat:@"-1,%@,%@,",answersARRAY[indexPath.row],imageStris];

    
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
        
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        
        [picker dismissViewControllerAnimated:YES completion:NULL];
    
    }

1
  • This is a new answer to an old question with an already accepted answer. What does your answer provided that the accepted answer does not? Additionally, adding an explanation on how this code works would help future visitors.
    – JAL
    Sep 8, 2016 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.