/* |
File: APLViewController.m |
Abstract: Main view controller for the application. |
Version: 2.0 |
|
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple |
Inc. ("Apple") in consideration of your agreement to the following |
terms, and your use, installation, modification or redistribution of |
this Apple software constitutes acceptance of these terms. If you do |
not agree with these terms, please do not use, install, modify or |
redistribute this Apple software. |
|
In consideration of your agreement to abide by the following terms, and |
subject to these terms, Apple grants you a personal, non-exclusive |
license, under Apple's copyrights in this original Apple software (the |
"Apple Software"), to use, reproduce, modify and redistribute the Apple |
Software, with or without modifications, in source and/or binary forms; |
provided that if you redistribute the Apple Software in its entirety and |
without modifications, you must retain this notice and the following |
text and disclaimers in all such redistributions of the Apple Software. |
Neither the name, trademarks, service marks or logos of Apple Inc. may |
be used to endorse or promote products derived from the Apple Software |
without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or |
implied, are granted by Apple herein, including but not limited to any |
patent rights that may be infringed by your derivative works or by other |
works in which the Apple Software may be incorporated. |
|
The Apple Software is provided by Apple on an "AS IS" basis. APPLE |
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS |
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND |
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
|
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL |
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, |
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED |
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), |
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE |
POSSIBILITY OF SUCH DAMAGE. |
|
Copyright (C) 2013 Apple Inc. All Rights Reserved. |
|
*/ |
|
#import "APLViewController.h" |
|
|
@interface APLViewController () |
|
@property (nonatomic, weak) IBOutlet UIImageView *imageView; |
|
@property (nonatomic, weak) IBOutlet UIToolbar *toolBar; |
|
@property (nonatomic) IBOutlet UIView *overlayView; |
@property (nonatomic, weak) IBOutlet UIBarButtonItem *takePictureButton; |
@property (nonatomic, weak) IBOutlet UIBarButtonItem *startStopButton; |
@property (nonatomic, weak) IBOutlet UIBarButtonItem *delayedPhotoButton; |
@property (nonatomic, weak) IBOutlet UIBarButtonItem *doneButton; |
|
@property (nonatomic) UIImagePickerController *imagePickerController; |
|
@property (nonatomic, weak) NSTimer *cameraTimer; |
@property (nonatomic) NSMutableArray *capturedImages; |
|
@end |
|
|
|
@implementation APLViewController |
|
|
- (void)viewDidLoad |
{ |
[super viewDidLoad]; |
|
self.capturedImages = [[NSMutableArray alloc] init]; |
|
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) |
{ |
// There is not a camera on this device, so don't show the camera button. |
NSMutableArray *toolbarItems = [self.toolBar.items mutableCopy]; |
[toolbarItems removeObjectAtIndex:2]; |
[self.toolBar setItems:toolbarItems animated:NO]; |
} |
} |
|
|
- (IBAction)showImagePickerForCamera:(id)sender |
{ |
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera]; |
} |
|
|
- (IBAction)showImagePickerForPhotoPicker:(id)sender |
{ |
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; |
} |
|
|
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType |
{ |
if (self.imageView.isAnimating) |
{ |
[self.imageView stopAnimating]; |
} |
|
if (self.capturedImages.count > 0) |
{ |
[self.capturedImages removeAllObjects]; |
} |
|
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; |
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; |
imagePickerController.sourceType = sourceType; |
imagePickerController.delegate = self; |
|
if (sourceType == UIImagePickerControllerSourceTypeCamera) |
{ |
/* |
The user wants to use the camera interface. Set up our custom overlay view for the camera. |
*/ |
imagePickerController.showsCameraControls = NO; |
|
/* |
Load the overlay view from the OverlayView nib file. Self is the File's Owner for the nib file, so the overlayView outlet is set to the main view in the nib. Pass that view to the image picker controller to use as its overlay view, and set self's reference to the view to nil. |
*/ |
[[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil]; |
self.overlayView.frame = imagePickerController.cameraOverlayView.frame; |
imagePickerController.cameraOverlayView = self.overlayView; |
self.overlayView = nil; |
} |
|
self.imagePickerController = imagePickerController; |
[self presentViewController:self.imagePickerController animated:YES completion:nil]; |
} |
|
|
#pragma mark - Toolbar actions |
|
- (IBAction)done:(id)sender |
{ |
// Dismiss the camera. |
if ([self.cameraTimer isValid]) |
{ |
[self.cameraTimer invalidate]; |
} |
[self finishAndUpdate]; |
} |
|
|
- (IBAction)takePhoto:(id)sender |
{ |
[self.imagePickerController takePicture]; |
} |
|
|
- (IBAction)delayedTakePhoto:(id)sender |
{ |
// These controls can't be used until the photo has been taken |
self.doneButton.enabled = NO; |
self.takePictureButton.enabled = NO; |
self.delayedPhotoButton.enabled = NO; |
self.startStopButton.enabled = NO; |
|
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0]; |
NSTimer *cameraTimer = [[NSTimer alloc] initWithFireDate:fireDate interval:1.0 target:self selector:@selector(timedPhotoFire:) userInfo:nil repeats:NO]; |
|
[[NSRunLoop mainRunLoop] addTimer:cameraTimer forMode:NSDefaultRunLoopMode]; |
self.cameraTimer = cameraTimer; |
} |
|
|
- (IBAction)startTakingPicturesAtIntervals:(id)sender |
{ |
/* |
Start the timer to take a photo every 1.5 seconds. |
|
CAUTION: for the purpose of this sample, we will continue to take pictures indefinitely. |
Be aware we will run out of memory quickly. You must decide the proper threshold number of photos allowed to take from the camera. |
One solution to avoid memory constraints is to save each taken photo to disk rather than keeping all of them in memory. |
In low memory situations sometimes our "didReceiveMemoryWarning" method will be called in which case we can recover some memory and keep the app running. |
*/ |
self.startStopButton.title = NSLocalizedString(@"Stop", @"Title for overlay view controller start/stop button"); |
[self.startStopButton setAction:@selector(stopTakingPicturesAtIntervals:)]; |
|
self.doneButton.enabled = NO; |
self.delayedPhotoButton.enabled = NO; |
self.takePictureButton.enabled = NO; |
|
self.cameraTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timedPhotoFire:) userInfo:nil repeats:YES]; |
[self.cameraTimer fire]; // Start taking pictures right away. |
} |
|
|
- (IBAction)stopTakingPicturesAtIntervals:(id)sender |
{ |
// Stop and reset the timer. |
[self.cameraTimer invalidate]; |
self.cameraTimer = nil; |
|
[self finishAndUpdate]; |
} |
|
|
- (void)finishAndUpdate |
{ |
[self dismissViewControllerAnimated:YES completion:NULL]; |
|
if ([self.capturedImages count] > 0) |
{ |
if ([self.capturedImages count] == 1) |
{ |
// Camera took a single picture. |
[self.imageView setImage:[self.capturedImages objectAtIndex:0]]; |
} |
else |
{ |
// Camera took multiple pictures; use the list of images for animation. |
self.imageView.animationImages = self.capturedImages; |
self.imageView.animationDuration = 5.0; // Show each captured photo for 5 seconds. |
self.imageView.animationRepeatCount = 0; // Animate forever (show all photos). |
[self.imageView startAnimating]; |
} |
|
// To be ready to start again, clear the captured images array. |
[self.capturedImages removeAllObjects]; |
} |
|
self.imagePickerController = nil; |
} |
|
|
#pragma mark - Timer |
|
// Called by the timer to take a picture. |
- (void)timedPhotoFire:(NSTimer *)timer |
{ |
[self.imagePickerController takePicture]; |
} |
|
|
#pragma mark - UIImagePickerControllerDelegate |
|
// This method is called when an image has been chosen from the library or taken from the camera. |
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info |
{ |
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; |
|
[self.capturedImages addObject:image]; |
|
if ([self.cameraTimer isValid]) |
{ |
return; |
} |
|
[self finishAndUpdate]; |
} |
|
|
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker |
{ |
[self dismissViewControllerAnimated:YES completion:NULL]; |
} |
|
|
@end |