This example shows how to Open Camera, Take Photo, Save it, Load it in iOS.

Camera Functions in IOS


Before this make sure that you have two buttons and one imageview in the Interface (image above) and link them to appropriate actions
and variables

ViewController.h”


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIImagePickerControllerDelegate>
{
    UIImagePickerController *imagePicker;
    IBOutlet UIImageView *imageView;
}

- (IBAction)showCamera:(id)sender;
- (IBAction)loadImage:(id)sender;

@end


ViewController.m



#import "ViewController.h"

@interface ViewController ()
{
    #define FILE_EXTENSION    @".png"
    #define IMAGE_NAME        @"CameraImage"
}
@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing = YES;
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Camera Unavailable"
                                                       message:@"Unable to find a camera on your device."
                                                      delegate:nil
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil, nil];
        [alert show];
        alert = nil;
    }
}

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

- (IBAction)showCamera:(id)sender {
    [self presentViewController:imagePicker animated:YES completion:nil];
}

+(NSString *)documentsPath:(NSString *)fileName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSLog(@"%@", fullPath);
    
    return fullPath;
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *fileSavePath = [ViewController documentsPath:IMAGE_NAME];
    fileSavePath = [fileSavePath stringByAppendingString:FILE_EXTENSION];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (image == nil) {
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    
    //This checks to see if the image was edited, if it was it saves the edited version as a .png
    if ([info objectForKey:UIImagePickerControllerEditedImage]) {
        //save the edited image
        NSData *imgPngData = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerEditedImage]);
        [imgPngData writeToFile:fileSavePath atomically:YES];
        
        
    }else{
        //save the original image
        NSData *imgPngData = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage]);
        [imgPngData writeToFile:fileSavePath atomically:YES];
        
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

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

- (IBAction)loadImage:(id)sender{
    NSString *path = [NSString stringWithFormat:@"%@%@",[ViewController documentsPath:IMAGE_NAME], FILE_EXTENSION];
    NSData *imgData = [NSData dataWithContentsOfFile:path];
    UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
    imageView.image = thumbNail;
}

@end

 

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.