Search This Blog

Loading...

Oct 7, 2010

How to select image from iPhone saved photo album or Camera.

This code is used for selecting image from iPhone Saved Photo Album or from iPhone Camera.


You can download source code from here Download.

1. Code for .h file.

#import

@interface RootViewController : UIViewController {
  
    IBOutlet UIImageView *imgPicture;
    NSData *dataImage;
}

- (IBAction)btnTakePicture_Clicked:(id)sender;

@end
2. Code for .m file.

#import "RootViewController.h"


@implementation RootViewController


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

}

- (IBAction)btnTakePicture_Clicked:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image from..." delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Image Gallary", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    actionSheet.alpha=0.90;
    actionSheet.tag = 1;
    [actionSheet showInView:self.view];
    [actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (actionSheet.tag)
    {
        case 1:
            switch (buttonIndex)
        {
            case 0:
            {              
#if TARGET_IPHONE_SIMULATOR
              
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Saw Them" message:@"Camera not available." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                [alert release];
              
#elif TARGET_OS_IPHONE  
              
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                picker.delegate = self;
                //picker.allowsEditing = YES;
                [self presentModalViewController:picker animated:YES];
                [picker release];
              
#endif  
            }
                break;
            case 1:
            {
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                picker.delegate = self;
                [self presentModalViewController:picker animated:YES];
                [picker release];
            }
                break;
        }
            break;
          
        default:
            break;
    }  
}
/*

    //    if you want to edit selected image then use this delegate method.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    imgPicture.image = image;
    [self.navigationController dismissModalViewControllerAnimated:YES];
}  
*/

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);
    imgPicture.image = [[UIImage alloc] initWithData:dataImage];
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  
    [self.navigationController dismissModalViewControllerAnimated:YES];  
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
  
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

3 comments:

  1. plz download code....

    ReplyDelete
  2. Hello,

    Code has been uploaded with the post.

    Enjoy iPhone development.

    ReplyDelete
  3. Its very interesting post, good job.

    videocon reacharge
    Videocon Telecom, the next-generation GSM mobile service network is the telecom arm of the $10 billion Videocon Group. The company has partnered with global technology leaders to create a robust infrastructure featuring future ready mobile technologies. Powered by the most advanced GSM-EDGE network, Videocon Telecom aims to redefine the Indian mobile telecommunications industry by leveraging a combination of the parent company’s strong brand recall and retail reach with a spirit of innovation and customer centricity. Videocon endeavours to transform the life of every Indian consumer by changing the very paradigm of the way he or she communicates.

    ReplyDelete