Showing posts with label Camera. Show all posts
Showing posts with label Camera. Show all posts

Tuesday, 11 November 2014

Swift + Pick Image From Photo Library Using UIImagePickerController

First Add and Import MobileCoreServices Framework.

Confirm to UIImagePickerControllerDelegate & UINavigationControllerDelegate protocol


import UIKit

import MobileCoreServices


class ViewController: UIViewControllerUIImagePickerControllerDelegateUIAlertViewDelegateUINavigationControllerDelegate
{

    var cameraUI: UIImagePickerController! = UIImagePickerController()
    
    
    override func viewDidLoad()
    {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        
    }

    //--- Take Photo from Photo Library ---//
    @IBAction func takePhotoFromPhotoLibrary(sender: AnyObject)
    {
          self.presentLibrary()
    }



    func presentLibrary()
    {
       
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)
        {
            cameraUI = UIImagePickerController()
            cameraUI.delegate = self
            cameraUI.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
            cameraUI.mediaTypes = [kUTTypeImage]
            cameraUI.allowsEditing = false
            
            self.presentViewController(cameraUI, animated: true, completion: nil)
        }
        else
        {
            // error msg

        }


    }
    
    //Mark- UIImagePickerController Delegate
    
    func imagePickerControllerDidCancel(picker:UIImagePickerController)
    {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    



    func imagePickerController(picker:UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary)
    {
       if(picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary)
        {
            var selectedImage: UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
            
            //show Image on Image View and do other stuf that you want
            
            self.dismissViewControllerAnimated(true, completion: nil)


        }
       
    }
 


 
}  

Swift + Take pictures and save to camera roll using UIImagePickerController

First Add and Import MobileCoreServices Framework.

Confirm to UIImagePickerControllerDelegate & UINavigationControllerDelegate protocol


import UIKit

import MobileCoreServices


class ViewController: UIViewController, UIImagePickerControllerDelegate, UIAlertViewDelegate, UINavigationControllerDelegate
{

    var cameraUI: UIImagePickerController! = UIImagePickerController()
    
    
    override func viewDidLoad()
    {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        
    }

    //--- Take Photo from Camera ---//
    @IBAction func takePhotoFromCamera(sender: AnyObject)
    {
          self.presentCamera()
    }



    func presentCamera()
    {
       
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
        {
            println("Button capture")
        
            cameraUI = UIImagePickerController()
            cameraUI.delegate = self
            cameraUI.sourceType = UIImagePickerControllerSourceType.Camera;
            cameraUI.mediaTypes = [kUTTypeImage]
            cameraUI.allowsEditing = false
            
            self.presentViewController(cameraUI, animated: true, completion: nil)
        }
        else
        {
            // error msg
        }
    }
    
    //Mark- UIImagePickerController Delegate
    
    func imagePickerControllerDidCancel(picker:UIImagePickerController)
    {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    



    func imagePickerController(picker:UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary)
    {
        if(picker.sourceType == UIImagePickerControllerSourceType.Camera)
        {
            // Access the uncropped image from info dictionary
            var imageToSave: UIImage = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage
            var imageToSave1: UIImage = info[UIImagePickerControllerOriginalImage] as UIImage //same but with different way
            
            UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil)
            
            self.savedImageAlert()
            self.dismissViewControllerAnimated(true, completion: nil)

        }
       
    }
    
    
    
    
    func savedImageAlert()
    {
        var alert:UIAlertView = UIAlertView()
        alert.title = "Saved!"
        alert.message = "Your picture was saved to Camera Roll"
        alert.delegate = self
        alert.addButtonWithTitle("Ok")
        alert.show()
    }





}

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];

}