Skip to content

Instantly share code, notes, and snippets.

@joebrislin
Last active December 10, 2015 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joebrislin/143a3acc940b4d904244 to your computer and use it in GitHub Desktop.
Save joebrislin/143a3acc940b4d904244 to your computer and use it in GitHub Desktop.
Code Samples - Mobile Application for Inventory Checkins (iOS / Objective C)
//
// PhotosViewController.h
// EMSCheck
//
// Created by Joe Brislin on 5/5/14.
// Copyright (c) 2014 BlueRidge Interactive. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <FUIButton.h>
#import "MasterViewController.h"
#import "ThemeButtonWithIcon.h"
#import <WSAssetPickerController/WSAssetPicker.h>
#import <SOAPEngine64/SOAPEngine.h>
@interface PhotosViewController : MasterViewController <UIImagePickerControllerDelegate,WSAssetPickerControllerDelegate>
{
UIImagePickerController *takePicker;
}
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) IBOutlet ThemeButtonWithIcon *takePhotoBtn;
@property (strong, nonatomic) IBOutlet ThemeButtonWithIcon *chooseExistingBtn;
@property (strong, nonatomic) FUIButton *submitBtn;
@property (strong, nonatomic) ALAssetsLibrary *assetsLibrary;
@property (strong, nonatomic) UIActivityIndicatorView *activityView;
@property (nonatomic, readwrite) BOOL pageControlInUse;
@property (nonatomic, readwrite) NSUInteger photoIndex;
@property (nonatomic, readwrite) NSInteger imageLimit;
- (IBAction)pickPhotos:(id)sender;
- (IBAction)takePhoto:(id)sender;
- (UIImagePickerController *)takePhotoWithCamera;
- (IBAction)changePage:(id)sender;
- (void)submitForm;
- (UIImage*)resizeImage:(UIImage *)img;
- (void)notifyInternetConnectivity;
@end
//
// PhotosViewController.m
// EMSCheck
//
// Created by Joe Brislin on 5/5/14.
// Copyright (c) 2014 BlueRidge Interactive. All rights reserved.
//
#import "PhotosViewController.h"
#import "AppDelegate.h"
#import "DataService.h"
#import <FontAwesome.h>
#import <TSMessages/TSMessage.h>
#import <UIColor+FlatUI.h>
#import "UIImage+Resize.h"
@interface PhotosViewController ()
@end
@implementation PhotosViewController {
AppDelegate *appDelegate;
CGFloat btnHeight;
}
@synthesize photoIndex, imageLimit;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)awakeFromNib
{
[self.tabBarItem setImage:[FontAwesome imageWithIcon:fa_camera iconColor:[UIColor whiteColor] iconSize:30.0f]];
self.tabBarItem.title = @"Photos";
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - self.tabBarController.tabBar.frame.size.height)];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.imageLimit = 8;
takePicker = [[UIImagePickerController alloc] init];
takePicker.delegate = self;
self.takePhotoBtn = [self.takePhotoBtn initWithFrame:self.takePhotoBtn.frame];
[self.takePhotoBtn setFontSize:20.0f :YES];
[self.takePhotoBtn setIcon:fa_camera :30.0f];
self.chooseExistingBtn = [self.chooseExistingBtn initWithFrame:self.chooseExistingBtn.frame];
[self.chooseExistingBtn setFontSize:20.0f :YES];
[self.chooseExistingBtn setIcon:fa_picture_o :30.0f];
btnHeight = 46.0f;
self.submitBtn = [[FUIButton alloc] initWithFrame: CGRectMake(0, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height - (btnHeight*1.55), self.view.frame.size.width, btnHeight)];
self.submitBtn.buttonColor = [UIColor nephritisColor];
self.submitBtn.shadowColor = [UIColor clearColor];
self.submitBtn.shadowHeight = 0.0f;
self.submitBtn.cornerRadius = 0.0f;
UIFont *textFont = [UIFont fontWithName:@"HelveticaNeue" size:14.0];
self.submitBtn.titleLabel.font = textFont;
[self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self.submitBtn setTitle:@"SUBMIT" forState:UIControlStateNormal];
[self.submitBtn setTitle:@"SUBMIT" forState:UIControlStateHighlighted];
self.submitBtn.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self.submitBtn addTarget:self action:@selector(submitForm) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.submitBtn];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[self setupScrollView]; // re-render images layout in scrollview
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyInternetConnectivity) name:@"notifyInternetConnectivity" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyInternetConnectivity" object:self];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ( [appDelegate.emsObject.emsType isEqualToString:@"INVENTORY CHECK"] || appDelegate.statusForm.selectableInd ) {
[self.submitBtn setHidden:NO];
}else{
[self.submitBtn setHidden:YES];
}
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
CGPoint scrollViewCenter = CGPointMake(self.scrollView.frame.size.width/2, self.scrollView.frame.size.height/2);
CGPoint activityCenter = [self.view convertPoint:scrollViewCenter fromView:self.scrollView];
self.activityView.center = activityCenter;
[self.submitBtn setFrame:CGRectMake(0, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height, self.view.frame.size.width, btnHeight)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (UIActivityIndicatorView *)activityView
{
if (_activityView == nil) {
_activityView =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[_activityView setColor:[UIColor darkGrayColor]];
[self.view addSubview:_activityView];
}
return _activityView;
}
- (void)setScrollView:(UIScrollView *)scrollView
{
scrollView.layer.cornerRadius = 4.f;
scrollView.layer.borderWidth = 1.f;
scrollView.layer.borderColor = [UIColor colorWithWhite:0.5 alpha:0.5].CGColor;
_scrollView = scrollView;
}
- (void)setPageControl:(UIPageControl *)pageControl
{
pageControl.numberOfPages = 0;
_pageControl = pageControl;
}
- (IBAction)pickPhotos:(id)sender {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
self.assetsLibrary = library;
WSAssetPickerController *picker = [[WSAssetPickerController alloc] initWithAssetsLibrary:library];
picker.delegate = self;
picker.selectionLimit = (self.photoIndex >= imageLimit) ? 0 : imageLimit - self.photoIndex;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)takePhoto:(id)sender {
takePicker = [self takePhotoWithCamera];
takePicker.view.tag = 91;
[self presentViewController:takePicker animated:YES completion:nil];
}
- (UIImagePickerController *)takePhotoWithCamera
{
[takePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[takePicker setEditing:NO];
return takePicker;
}
- (IBAction)changePage:(id)sender {
self.pageControlInUse = YES;
CGRect pageFrame = self.scrollView.bounds;
pageFrame.origin.x = pageFrame.size.width * self.pageControl.currentPage;
[self.scrollView scrollRectToVisible:pageFrame animated:YES];
}
- (void)setupScrollView
{
// ScrollView setup.
NSLog(@"[PhotosViewController] Setting up scroll view...");
NSLog(@"[PhotosViewController] AppDelegate Photos (%lu) - %@",(unsigned long)[appDelegate.photos count], appDelegate.photos);
self.photoIndex = [appDelegate.photos count];
if (!self.photoIndex) {
[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
[self checkPhotoLimit];
CGFloat padding = 10.0;
CGRect imageViewFrame = CGRectInset(self.scrollView.bounds, padding, padding);
imageViewFrame.origin.x = self.scrollView.frame.size.width * self.photoIndex + padding;
CGSize contentSize = CGSizeZero;
contentSize.width = self.scrollView.frame.size.width * self.photoIndex;
contentSize.height = self.scrollView.frame.size.height-(padding*2);
self.scrollView.contentSize = contentSize;
// PageControl setup.
self.pageControl.hidden = NO;
self.pageControl.numberOfPages = self.photoIndex;
for (UIImage *image in appDelegate.photos) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = imageViewFrame;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:imageView];
}
[self.activityView stopAnimating];
[self.scrollView flashScrollIndicators];
}
- (void)submitForm
{
[self processEMSCheck:appDelegate.statusForm.tagNumber photos:appDelegate.photos dismissView:YES];
}
- (UIImage*)resizeImage:(UIImage *)img
{
CGFloat i_width = 1000.0f;
if (img.size.width > i_width) {
float scale = i_width / img.size.width;
CGFloat i_height = img.size.height * scale;
CGSize newSize = CGSizeMake(i_width, i_height);
return [img resizedImage:newSize interpolationQuality:kCGInterpolationHigh];
}else{
return img;
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Show some activity.
[self.activityView startAnimating];
CGFloat padding = 10.0;
CGRect imageViewFrame = CGRectInset(self.scrollView.bounds, padding, padding);
imageViewFrame.origin.x = self.scrollView.frame.size.width * self.photoIndex + padding;
UIImage *image = [self resizeImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
[appDelegate.photos addObject:image];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = imageViewFrame;
imageView.contentMode = UIViewContentModeScaleAspectFit;
self.photoIndex++;
[self.scrollView addSubview:imageView];
[self dismissViewControllerAnimated:YES completion:nil];
// ScrollView setup.
CGSize contentSize = CGSizeZero;
contentSize.width = self.scrollView.frame.size.width * self.photoIndex;
contentSize.height = self.scrollView.frame.size.height-(padding*2);
self.scrollView.contentSize = contentSize;
// PageControl setup.
self.pageControl.hidden = NO;
self.pageControl.numberOfPages = self.photoIndex;
[self.activityView stopAnimating];
[self.scrollView flashScrollIndicators];
[self checkPhotoLimit];
}
- (void)checkPhotoLimit {
if (self.photoIndex >= 8) {
self.takePhotoBtn.enabled = NO;
self.takePhotoBtn.alpha = 0.5;
self.chooseExistingBtn.enabled = NO;
self.chooseExistingBtn.alpha = 0.5;
}else{
self.takePhotoBtn.enabled = YES;
self.takePhotoBtn.alpha = 1.0;
self.chooseExistingBtn.enabled = YES;
self.chooseExistingBtn.alpha = 1.0;
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - WSAssetPickerControllerDelegate Methods
- (void)assetPickerControllerDidCancel:(WSAssetPickerController *)sender
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
// Reset the scroll view and page control.
// [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// self.scrollView.contentSize = CGSizeZero;
// self.pageControl.numberOfPages = 0;
self.pageControl.currentPage = 0;
// self.pageControl.hidden = YES;
// Show some activity.
[self.activityView startAnimating];
// Dismiss the picker controller.
[self dismissViewControllerAnimated:YES completion:^{
if (assets.count == 0) {
[self.activityView stopAnimating];
[self dismissViewControllerAnimated:YES completion:nil];
return;
}
CGFloat padding = 10.0;
// ScrollView setup.
self.photoIndex = [appDelegate.photos count];
CGFloat assetsCount = self.photoIndex + assets.count;
CGSize contentSize = CGSizeZero;
contentSize.width = self.scrollView.frame.size.width * assetsCount;
contentSize.height = self.scrollView.frame.size.height-(padding*2);
self.scrollView.contentSize = contentSize;
// PageControl setup.
self.pageControl.hidden = NO;
self.pageControl.numberOfPages = assetsCount;
for (ALAsset *asset in assets) {
CGRect imageViewFrame = CGRectInset(self.scrollView.bounds, padding, padding);
imageViewFrame.origin.x = self.scrollView.frame.size.width * self.photoIndex + padding;
UIImage *image = [self resizeImage:[[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage]];
[appDelegate.photos addObject:image];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = imageViewFrame;
imageView.contentMode = UIViewContentModeScaleAspectFit;
self.photoIndex++;
[self.scrollView addSubview:imageView];
}
[self.activityView stopAnimating];
[self.scrollView flashScrollIndicators];
[self checkPhotoLimit];
}];
}
#pragma mark - UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// Update the pageControl > 50% of the previous/next page is visible.
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.pageControlInUse = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
self.pageControlInUse = NO;
}
- (void)notifyInternetConnectivity
{
if ([[AppDelegate sharedDelegate] isReachable]) {
self.submitBtn.enabled = YES;
self.submitBtn.alpha = 1.0;
}else{
self.submitBtn.enabled = NO;
self.submitBtn.alpha = 0.5;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment