Saturday, 23 February 2013

To open gallery or camera programmatically and get selected photo in delegate method in IOS


-(void)Open_Camera
{
    
    NSString *model = [[UIDevice currentDevice] model];
    if ([model isEqualToString:@"iPhone Simulator"] || [model isEqualToString:@"iPad Simulator"])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Angry!!!" message:@"No Camera found!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }
    else
    {
        
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
       
       [self presentModalViewController: picker animated:YES];

        
        picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        
    }
}
#pragma mark Method_Open_Library
-(void)Open_Library
{
    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    
    // Set source to the camera
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate=self;
    // Delegate is self
    
    // Allow editing of image ?
    imagePicker.allowsEditing = YES;
    // Show image picker
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
        CGRect popoverRect = CGRectMake(10, 10, 200, 200);
        
        [popover presentPopoverFromRect:popoverRect inView:self.contentView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        
    }
    else
    {
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
    
    
}

#pragma mark imagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    
    UIImage *image_answer = [editingInfo objectForKey:UIImagePickerControllerOriginalImage];
   
  
}