10 Feb 2017

UIImagePickerController & NSUserDefaults Full Code…


UIImagePickerController & NSUserDefaults Full Code…



Add it in Source Code of info.plist...

(<key>NSPhotoLibraryUsageDescription</key>
    <string>$(PRODUCT_NAME) uses photos</string> )



StorYboard...



#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *firstNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *lastNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UIImageView *contactImageView;

- (IBAction)saveBtn:(id)sender;
- (IBAction)chooseImageBtn:(id)sender;

@end


#import "ViewController.h"

@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    
    // Get the stored data before the view loads
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSString *firstName = [defaults objectForKey:@"firstName"];
    NSString *lastName = [defaults objectForKey:@"lastname"];
    
    NSInteger age = [defaults integerForKey:@"age"];
    NSString *ageString = [NSString stringWithFormat:@"%li",(long)age];
    
    NSData *imageData = [defaults dataForKey:@"image"];
    UIImage *contactImage = [UIImage imageWithData:imageData];
    
    // Update the UI elements with the saved data
    _firstNameTextField.text = firstName;
    _lastNameTextField.text = lastName;
    _ageTextField.text = ageString;
    _contactImageView.image = contactImage;
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)saveBtn:(id)sender {
    
    // Hide the keyboard
    [_firstNameTextField resignFirstResponder];
    [_lastNameTextField resignFirstResponder];
    [_ageTextField resignFirstResponder];
    
    // Create strings and integer to store the text info
    NSString *firstName = [_firstNameTextField text];
    NSString *lastName  = [_lastNameTextField text];
    NSInteger age = [[_ageTextField text] integerValue];
    
    // Create instances of NSData
    UIImage *contactImage = _contactImageView.image;
    NSData *imageData = UIImageJPEGRepresentation(contactImage, 100);
    
    
    // Store the data
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setObject:firstName forKey:@"firstName"];
    [defaults setObject:lastName forKey:@"lastname"];
    [defaults setInteger:age forKey:@"age"];
    [defaults setObject:imageData forKey:@"image"];
    
    [defaults synchronize];
    
    NSLog(@"Data saved");
    
}

- (IBAction)chooseImageBtn:(id)sender {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil];
    
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    _contactImageView.image = image;
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

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

No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0