Monday, 8 September 2014

Open Camera and Gallery to pick a image through Picker View and Action Sheet

Step 1: Import UIActionSheetDelegate,UIImagePickerControllerDelegate,UIPickerViewDelegate,UIPickerViewDataSource,UINavigationControllerDelegate

//in .h file

NSInteger selectedOption;

@property(nonatomic,strong) UIPopoverController *customPopoverController;

@property (nonatomic , strong) NSArray *arrayImageUploadOption;


//.m file

    if(ISIphone)
        self.arrayImageUploadOption = @[@"iPhone Gallery",@"iPhone Camera"];
    else
        self.arrayImageUploadOption = @[@"iPad Gallery",@"iPad Camera"];


//On button click we open ActionSheet

-(IBAction)buttonAvatarPressed:(id)sender
{
    [self createActionSheet];
}

-(void)createActionSheet
{
    
    NSString *stringTitle=@"Pick image from:";

    UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:stringTitle delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
  
    [asheet showInView:[self.view superview]];
    
    [asheet setFrame:CGRectMake(0, 117, 320, 383)];

}


//Now we implement Action Sheet delegate methods and add picker view on action sheet

#pragma Marks - UIActionSheet Delegate

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    //create picker view

    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
    
    [pickerView setDataSource: self];
    [pickerView setDelegate: self];
    
    pickerView.showsSelectionIndicator = YES;
    
    [actionSheet addSubview:pickerView];
    
    
    //Gets an array of all of the subviews of our actionSheet
    NSArray *subviews = [actionSheet subviews];
    
    if(ISIOS7OrGreater)
    {
        [[subviews objectAtIndex:1] setFrame:CGRectMake(20, 0, 280, 46)];
        [[subviews objectAtIndex:2] setFrame:CGRectMake(20, 266, 280, 46)];
        [[subviews objectAtIndex:3] setFrame:CGRectMake(20, 317, 280, 46)];
        
    }
    else
    {
        [[subviews objectAtIndex:1] setFrame:CGRectMake(20, 266, 280, 46)];
        [[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
        
    }
    
    
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [actionSheet cancelButtonIndex])
    {
        if(selectedOption == 0)
            [self openGallery];

        if (selectedOption == 1)
            [self openCamera];
        
    }

}


#pragma marks - UIPickerView Delegate

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    //Two columns
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.arrayImageUploadOption count];
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.arrayImageUploadOption objectAtIndex:row];
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //NSLog(@"Selected Row %d", row);
    selectedOption = [pickerView selectedRowInComponent:0];
    
    

}



// Now here we open Camera or Gallery

-(void)openGallery
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init] ;
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = TRUE;
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        
        if(ISIphone)
        {
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }
        else
        {
            //imagePickerController.contentSizeForViewInPopover = CGSizeMake(400, 1000); //deprecated
            
            [imagePickerController setPreferredContentSize:CGSizeMake(400, 1000)];
            
            self.customPopoverController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
            
            [self.customPopoverController presentPopoverFromRect:CGRectMake(-10.0, -400.0, 400.0, 400.0)
                                                          inView:self.view
                                        permittedArrowDirections:UIPopoverArrowDirectionAny
                                                        animated:YES];
            
        }
    }
    
    
}




-(void)openCamera
{
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init] ;
    
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
        
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = TRUE;
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
    
    

}



// Set Picked image on Image View

#pragma marks - UIImagePicker Delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //NSLog(@"image picked:");
    UIImage *imageLoc = [[UIImage alloc] init];
    imageLoc = [info objectForKey:UIImagePickerControllerEditedImage];
    //NSData *imageData = UIImagePNGRepresentation(imageLoc);
    
    //show picked image on image view
    self.imgView.image= imageLoc;
  
    //dismiss view controller in case of iPhone
    //remove popover in case of iPad
    if(ISIphone)
        [picker dismissViewControllerAnimated:YES completion:nil];
    else
        [self.customPopoverController dismissPopoverAnimated:YES];

}




2 comments:

  1. Articles, especially those published on reputable news sites or academic journals, Will Gojo Satoru Die In Jujutsu Kaisen? undergo rigorous editing and fact-checking processes to ensure accuracy and credibility.

    ReplyDelete